我想通过 PHP 保存图像。我的代码是这样的:
$image = imagecreatefrompng("myimage.png");
$width = imagesx($image);
$height = imagesy($image);
if ($width == 0) {
$thumb_width = 0;
$thumb_height = 0;
} else {
$thumb_width = 600;
$thumb_height = (int)(600 * $height / $width);
}
$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;
if ( $original_aspect >= $thumb_aspect )
{
// If image is wider than thumbnail (in aspect ratio sense)
$new_height = $thumb_height;
$new_width = (int)($width / ($height / $thumb_height));
}
else
{
// If the thumbnail is wider than the image
$new_width = $thumb_width;
$new_height = $thumb_height;
}
$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );
// Resize and crop
imagecopyresampled($thumb,
$image,
0, 0,
0, 0,
$new_width, $new_height,
$width, $height);
imagejpeg($thumb, "newimage.jpg", 80);
有一个问题,newimage.jpg 比 png 更暗 - 为什么,我应该怎么做才能正确保存它。
有没有办法用新的不透明度保存 newimage.jpg?- 我怎样才能做到这一点?
谢谢 :)