gpt4 book ai didi

PHP上传导致图像模糊

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

我正在使用可在此处找到的裁剪工具:https://github.com/sconsult/croppic

我已经修改它以满足我的需要 - 对代码量表示歉意,但我不知道它的哪一部分可能会导致问题。

我大约在 5 到 6 个月前首次实现了这段代码,它运行得非常完美。 85% 的质量对于每张测试的图像来说都恰到好处。 自最初实现以来,代码没有改变,但输出的图像非常明显模糊 - 它们看起来不再漂亮了。我用之前上传的相同图片进行了测试以进行比较,它们看起来与最初上传和裁剪后的图片不一样。

据我所知,唯一可能对此产生影响的重大事件是升级我们的 PHP 版本。我相信在实现时它曾经是 5.3xx 或类似版本,现在我们正在运行 5.6.18。什么可能导致模糊? 5.6.18 相对于 5.3 是否有任何可归因于此的显着变化?

请注意,我已尝试将 $jpeg_quality = 85; 更改为 100。不幸的是,所有这些都会增加文件大小。出现同样的模糊。

<?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);

更新

下面是 2 个示例图像,第一个是调整后的版本(质量差,但设置为 100% jpeg 质量输出),第二个是原始图像。看看墙上的石头,质量下降非常明显。

裁剪

enter image description here

原创

enter image description here

最佳答案

添加

imageconvolution($original_img,array(array(-1,-1,-1),array(-1,16,-1),array(-1,-1,-1)),8,0);

在 imagecopyresample 之后和 imagejpeg 之前

图像卷积用于锐化图像

关于PHP上传导致图像模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599616/

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