【问题标题】:Photo has wrong colors after resized with PHP script使用 PHP 脚本调整大小后照片颜色错误
【发布时间】:2018-07-14 18:24:34
【问题描述】:

我正在使用以下 PHP 函数来调整大图像的大小以适应 500 像素的宽度:

<?php
function resizeImage($name) {
header('Content-type: image/jpeg');
$filename = "file.jpg";
$new_width = 500;
list($width, $height) = getimagesize($filename);
$new_height = (($height*$new_width)/$width);
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, "file.jpg", 100);
}
?>

由于某种原因,调整大小的图像上的颜色与以前不完全相同。它们不像以前那样清晰和强大。如您所见[图片已删除],左侧(原始)照片中的红色和亮度更高。

为什么?我的脚本有问题吗?还是正常的大小调整效果?

【问题讨论】:

  • 原图是RGB还是CMYK?
  • @MarioWerner 这是用 iPhone 拍摄的。原始的和调整大小的都是 RGB。
  • 虽然两个图像都是 RGB,但它们可以有不同的色彩空间,如 sRGB 或 Adob​​eRGB。我不知道您使用的 PHP 函数是否可以正确处理颜色空间/颜色配置文件或转换 - 我建议将此作为进一步研究的起点。
  • 看起来这个问题已经在 stackoverflow 上得到了回答:stackoverflow.com/questions/5773032/…
  • @Benni 谢谢,但这似乎只适用于imagemagick。但我不使用它。知道该怎么做吗?

标签: php image jpeg


【解决方案1】:

这是我现在的工作代码:

<?php
// Call the function with: resizeImage("INSERT_YOUR_FILE_NAME_INCLUDING_SUFFIX_HERE");
function resizeImage($file_name) {

// File is located at: files/original/
$filename = "files/original/".$file_name;

// The width you want the converted image has
$new_width = 500;

// Calculate right height
list($width, $height) = getimagesize($filename);
$new_height = (($height*$new_width)/$width);

// Get image
$small = new Imagick($filename);

// Resize image, but only if original image is wider what the wanted 500 px
if($width > $new_width) {$small->resizeImage($new_width, $new_height, Imagick::FILTER_LANCZOS, 1);}

// Some code to correct the color profile
$version = $small->getVersion();
$profile = "sRGB_IEC61966-2-1_no_black_scaling.icc";
if((is_array($version) === true) && (array_key_exists("versionString", $version) === true)) {$version = preg_replace("~ImageMagick ([^-]*).*~", "$1", $version["versionString"]);if(is_file(sprintf("/usr/share/ImageMagick-%s/config/sRGB.icm", $version)) === true) {$profile = sprintf("/usr/share/ImageMagick-%s/config/sRGB.icm", $version);}}if(($srgb = file_get_contents($profile)) !== false){$small->profileImage("icc", $srgb);$small->setImageColorSpace(Imagick::COLORSPACE_SRGB);}

// Safe the image to: files/small/
$small->writeImage("files/small/".$file_name);

// Clear all resources associated to the Imagick object
$small->clear();
}
?>

不要忘记从http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc下载icc文件并将其保存在与调整大小文件相同的目录中,或者将$profile = "sRGB_IEC61966-2-1_no_black_scaling.icc";更改为$profile = "http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2013-05-18
    • 2020-09-09
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多