gpt4 book ai didi

php - 图像调整大小 : Poor jpeg quality and black PNG backgrounds

转载 作者:行者123 更新时间:2023-12-02 05:13:51 25 4
gpt4 key购买 nike

最后:我决定基本上使用这个:http://shiftingpixel.com/2008/03/03/smart-image-resizer/

因为它处理所有事情,我关闭了缓存并在管理 Controller 中执行此操作:

    $image = file_get_contents(SITE_ADMIN_IMAGE.'/SmartImage.php?width='.$this->thumb_width.'&height='.$this->thumb_height.'&image=/images/'.$this->image_directory.'/'.$formData['image_url'].'');
file_put_contents(ROOT_PATH.'/public/images/'.$this->image_directory.'/thumb/'.$formData['image_url'], $image);

编辑:我发现这可行,但它会产生非常锐利的边缘,看起来不对。

    imagecolortransparent($dstImage, $background);
imagealphablending($dstImage, false);
$colorTransparent = imagecolorallocatealpha($dstImage, 0, 0, 0, 127);
imagefill($dstImage, 0, 0, $colorTransparent);
imagesavealpha($dstImage, true);
imagepng($dstImage, $toWhere);

想法?

你好,

我的类(class)有两个问题,基本上 jpeg 图像的质量很差,但我不确定这是否与我的比例调整有关。理想情况下,我希望这门课对图像尺寸有严格要求并对其进行裁剪,但我无法理解它。

我的主要问题是 png 总是有黑色背景,有没有人遇到过这种情况?

<?php


class OpenSource_ImageResize {


function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight) {

if ($mime == NULL) {
$mime = getimagesize($theFile);
$mime = $mime['mime'];
}


if ($mime == 'image/jpeg') {
$size = getimagesize($theFile);

if ($size[0] > $newWidth || $size[1] > $newHeight) {
$sourceImage = imagecreatefromjpeg($theFile);
} else {
return copy($theFile, $toWhere);
throw new exception('Could not create jpeg');
return false;
}
} else if ($mime == 'image/png') {
$size = getimagesize($theFile);

if ($size[0] > $newWidth || $size[1] > $newHeight) {
$sourceImage = imagecreatefrompng($theFile);
} else {
return copy($theFile, $toWhere);
//throw new exception('Could not create png');
return false;
}
} else if ($mime == 'image/gif') {
$size = getimagesize($theFile);

if ($size[0] > $newWidth || $size[1] > $newHeight) {
$sourceImage = imagecreatefromgif ($theFile);
} else {
return copy($theFile, $toWhere);
//throw new exception('Could not create gif');
return false;
}
} else {
throw new exception('Not a valid mime type');
return false;
}

$oldX = imageSX($sourceImage);
$oldY = imageSY($sourceImage);

if ($newWidth == NULL) {
$thumbHeight = $newHeight;
$thumbWidth = round($newHeight/($oldY/$oldX));
} else

if ($oldX > $oldY) {
$thumbWidth = $newWidth;
$thumbHeight = $oldY * ($newHeight/$oldX);
}

if ($oldX < $oldY) {
$thumbWidth = round($newHeight/($oldY/$oldX));
$thumbHeight = $newHeight;
}

if ($oldX == $oldY) {
$thumbWidth = $newWidth;
$thumbHeight = $newHeight;
}

if (!gd_info()) {
$dstImage = ImageCreate($thumbWidth, $thumbHeight);
imagecopyresized($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
} else {
$dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);
imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldX, $oldY);
}

if ($mime == 'image/png') {
$xparent = imagecolorresolvealpha($dstImage, 255,2,240, 0) ;
imagecolortransparent($dstImage,$xparent);
imagealphablending($dstImage,true);
imagepng($dstImage, $toWhere);

} else if ($mime == 'image/jpeg') {
imagejpeg($dstImage, $toWhere);

} else if ($mime == 'image/gif') {
imagegif ($dstImage, $toWhere);

}

imagedestroy($dstImage);
imagedestroy($sourceImage);
return true;
}

}

最佳答案

关于 JPEG 图像质量,您需要使用 imagejpeg() 中的第三个参数。 :

imagejpeg($dstImage, $toWhere, 90); // any value above 85 should be fine

关于 PNG 透明度,你做错了。您的脚本很糟糕并且存在根本性问题,我会给您留下一个修改后的脚本来解决您的两个问题。它仍然可以进一步优化,但我选择留下一些你原来不太重要的错误,这样你就不会感到迷茫:

class OpenSource_ImageResize
{
// $extension is not used?
function __construct($theFile, $toWhere, $mime, $extension, $newWidth, $newHeight)
{
$sourceImage = ImageCreateFromString(file_get_contents($theFile));

if (is_resource($sourceImage))
{
$info = getimagesize($theFile);

if (is_null($mime))
{
$mime = $info['mime'];
}

if ($info[0] <= $newWidth && $info[1] <= $newHeight)
{
imagedestroy($sourceImage);
return copy($theFile, $toWhere);
}

if (is_null($newWidth))
{
$thumbHeight = $newHeight;
$thumbWidth = round($newHeight/($info[1]/$info[0]));
}

else if ($info[0] > $info[1])
{
$thumbWidth = $newWidth;
$thumbHeight = $info[1] * ($newHeight/$info[0]);
}

if ($info[0] < $info[1])
{
$thumbWidth = round($newHeight/($info[1]/$info[0]));
$thumbHeight = $newHeight;
}

if ($info[0] == $info[1])
{
$thumbWidth = $newWidth;
$thumbHeight = $newHeight;
}

$dstImage = ImageCreateTrueColor($thumbWidth, $thumbHeight);

/* fix PNG transparency issues */
ImageFill($dstImage, 0, 0, IMG_COLOR_TRANSPARENT);
ImageSaveAlpha($dstImage, true);
ImageAlphaBlending($dstImage, true);

imagecopyresampled($dstImage, $sourceImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $info[0], $info[1]);

switch ($mime)
{
case 'image/png':
imagepng($dstImage, $toWhere, 9); // compress it (level 1 to 9)
break;

case 'image/jpeg':
imagejpeg($dstImage, $toWhere, 90); // quality = 90 (1 to 100, default is "about" 75)
break;

case 'image/gif':
imagegif($dstImage, $toWhere);
break;
}

imagedestroy($dstImage);
imagedestroy($sourceImage);

return true;
}
}
}

很抱歉没有明确指出您的错误,但错误太多了,现在是凌晨 3 点,需要睡一觉 - 学习它并阅读手册,如果您有任何疑问,请告诉我。

关于php - 图像调整大小 : Poor jpeg quality and black PNG backgrounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2832949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com