我使用的裁剪工具,可以在这里找到:https://github.com/sconsult/croppicPHP上传导致图像模糊
我已经修改了它适合我的需要 - 对代码的数量道歉,但我不知道它的哪一部分可能会引起问题。
我第一次在5 - 6个月前实现了这个代码,它的工作完美无瑕。 85%的质量适合每个测试图像。 代码没有改变,因为它的原始实现,但图像出现非常明显模糊 - 他们只是不再看起来不错。我测试过以前上传过的相同图像进行比较,它们看起来与最初上传和裁剪的图像不一样。
我知道的唯一可能会影响到的主要事件是升级版本PHP
。我相信它在执行时曾经是5.3xx
或类似的,现在我们正在运行5.6.18
。什么可能导致模糊? 5.6.18
5.3
有什么明显的变化可以归因于此?
请注意,我试图改变$jpeg_quality = 85;
。不幸的是,所有这些都会增加文件的大小。同样的模糊发生。
<?php
$image = $_POST['imgUrl'];
// original sizes
$img_init_w = $_POST['imgInitW'];
$img_init_h = $_POST['imgInitH'];
// resized sizes
$img_w = $_POST['imgW'];
$img_h = $_POST['imgH'];
// offsets
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
// crop box
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
// rotation angle
$angle = $_POST['rotation'];
$id = $_POST['id'];
$dtormob = $_POST['dtormob'];
$jpeg_quality = 85;
$original_filename = "../../..".WEB_ROOT."lib/img/$id-original";
// different filenames depending on whether this is for mobile or desktop
switch ($dtormob)
{
case 'dt':
$cropped_lrg_filename = "../../..".WEB_ROOT."lib/img/$id-lrg-w"; // 1440 x 568
$cropped_sml_filename = "../../..".WEB_ROOT."lib/img/$id-med-w"; // 767 x 302
$lrg_resized_w = 1440;
$lrg_resized_h = 568;
$sml_resized_w = 767;
$sml_resized_h = 302;
break;
case 'mob':
$cropped_lrg_filename = "../../..".WEB_ROOT."lib/img/$id-med-s"; // 767 x 607
$cropped_sml_filename = "../../..".WEB_ROOT."lib/img/$id-sml-s"; // 480 x 380
$lrg_resized_w = 767;
$lrg_resized_h = 607;
$sml_resized_w = 480;
$sml_resized_h = 380;
break;
}
$what = getimagesize($image);
switch(strtolower($what['mime']))
{
case 'image/png':
$img_r = imagecreatefrompng($image);
$source_image = imagecreatefrompng($image);
$type = '.png';
break;
case 'image/jpeg':
$img_r = imagecreatefromjpeg($image);
$source_image = imagecreatefromjpeg($image);
error_log('jpg');
$type = '.jpeg';
break;
case 'image/gif':
$img_r = imagecreatefromgif($image);
$source_image = imagecreatefromgif($image);
$type = '.gif';
break;
default: die('image type not supported');
}
// Error checking
if (!is_writable(dirname($cropped_lrg_filename))) {
$response = array(
'status' => 'error',
'message' => "Can't write cropped File, directory inaccesible"
);
}
else if ($what[0] > 2000 || $what[1] > 2000) {
$response = array(
'status' => 'error',
'message' => '<strong>Image too large</strong>, please upload an image <strong>2000 x 2000px</strong> or less.<br /><em>(No smaller than '.$lrg_resized_w.' x '.$lrg_resized_h.'px)</em>'
);
}
else if ($what[0] < $lrg_resized_w || $what[1] < $lrg_resized_h) {
$response = array(
'status' => 'error',
'message' => '<strong>Image too small</strong>, please upload an image <strong>'.$lrg_resized_w.' x '.$lrg_resized_h.'px</strong> or more.<br /><em>(No bigger than 2000 x 2000px)</em>'
);
}
else if ($what['channels'] > 3) {
$response = array(
'status' => 'error',
'message' => '<strong>Image format error</strong>, this image is in CMYK format, please convert it to <strong>RGB</strong> first.'
);
}
else
{
$original_filename_absolute = PROTOCOL.SUB_DOMAIN.DOMAIN.str_replace('../../..', '', $original_filename).$type;
$cropped_lrg_filename_absolute = PROTOCOL.SUB_DOMAIN.DOMAIN.str_replace('../../..', '', $cropped_lrg_filename).$type;
$cropped_sml_filename_absolute = PROTOCOL.SUB_DOMAIN.DOMAIN.str_replace('../../..', '', $cropped_sml_filename).$type;
// remove any images that already exist
if (file_exists($original_filename.$type)) {
unlink($original_filename.$type);
}
if (file_exists($cropped_lrg_filename.$type)) {
unlink($cropped_lrg_filename.$type);
}
if (file_exists($cropped_sml_filename.$type)) {
unlink($cropped_sml_filename.$type);
}
// lets make a copy of the original
// ====================================================================================
$original_img = imagecreatetruecolor($img_init_w, $img_init_h);
imagecopyresampled($original_img, $source_image, 0, 0, 0, 0, $img_init_w, $img_init_h, $img_init_w, $img_init_h);
imagejpeg($original_img, $original_filename.$type, 100);
// create large crop first - then resize after as it should be the same aspect ratio
// ====================================================================================
// resize the original image to size of editor
$lrg_resized_image = imagecreatetruecolor($img_w, $img_h);
imagecopyresampled($lrg_resized_image, $source_image, 0, 0, 0, 0, $img_w, $img_h, $img_init_w, $img_init_h);
// rotate the rezized image
$rotated_image = imagerotate($lrg_resized_image, -$angle, 0);
// find new width & height of rotated image
$rotated_width = imagesx($rotated_image);
$rotated_height = imagesy($rotated_image);
// diff between rotated & original sizes
$dx = $rotated_width - $img_w;
$dy = $rotated_height - $img_h;
// crop rotated image to fit into original rezized rectangle
$cropped_rotated_image = imagecreatetruecolor($img_w, $img_h);
imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx/2, $dy/2, $img_w, $img_h, $img_w, $img_h);
// crop image into selected area
$final_image = imagecreatetruecolor($cropW, $cropH);
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
// finally output image
imagejpeg($final_image, $cropped_lrg_filename.$type, $jpeg_quality);
// now create smaller crop - same aspect ratio just smaller
// ====================================================================================
$sml_resized_image = imagecreatetruecolor($sml_resized_w, $sml_resized_h);
imagecopyresampled($sml_resized_image, $final_image, 0, 0, 0, 0, $sml_resized_w, $sml_resized_h, $cropW, $cropH);
// save it
imagejpeg($sml_resized_image, $cropped_sml_filename.$type, $jpeg_quality);
$response = array(
'status' => 'success',
'url' => $cropped_lrg_filename.$type
);
}
echo json_encode($response);
UPDATE
下面是2的示例图像,所述第一被调整大小后的版本(质量差,但设定为100%JPEG画质输出),第二个是原始图像。看看墙上的石头,质量的下降非常明显。
裁剪
原
通过这篇文章了解了很多 –