【问题标题】:PHP GD cropping PNG doesn't workPHP GD裁剪PNG不起作用
【发布时间】:2014-08-03 03:23:53
【问题描述】:

我正在创建一个使用 PHP GD 裁剪图像的函数。 该函数非常适用于 JPEG 图像,但是当我尝试对 PNG 使用相同的函数时,当我在 chrome 中检查页面时,我只会得到一个空白页面和以下错误:

我意识到这是一个 javascript 错误,但这可能是 chrome 所做的事情?这就是我访问 IMAGE URL 时发生的情况。如果我用 JPEG 代码复制它,它会完美运行。

编辑:在 Firefox 中,它说“无法显示图像 [URL],因为它包含错误。下面的 js 错误只是一个 chrome 错误,并不是很相关。

未捕获的类型错误:无法读取 null 的属性“getAttribute” data_loader.js:2 未捕获类型错误:无法读取属性 'hasAttribute' 的 null global-shortcut.js:9

工作JPEG代码如下:

$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];

$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefromjpeg($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;

$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;

$width = imagesx($image);
$height = imagesy($image);

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0,
                   0, 
                   $_POST['x'] / $ratio, $_POST['y'] / $ratio,
                   $width, $height,
                   $width, $height);

imagejpeg($thumb, $target, $jpeg_quality);

破碎的PNG代码如下:

$fileName = $_POST['file'];
$jpeg_quality = 100;
$ratio = $_POST['r'];

$src = '/var/www/admin/public_html/images/'.$fileName;
$image = imagecreatefrompng($src);
$target = '/var/www/admin/public_html/images/crop/'.$fileName;

$thumb_width = $_POST['w'] / $ratio;
$thumb_height = $_POST['h'] / $ratio;

$width = imagesx($image);
$height = imagesy($image);

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0,
                   0, 
                   $_POST['x'] / $ratio, $_POST['y'] / $ratio,
                   $width, $height,
                   $width, $height);

imagepng($thumb, $target, $jpeg_quality);

【问题讨论】:

  • 这是一个 JS 错误...您需要显示该代码。
  • 我意识到这是一个 JS 错误但没有代码,这就是我访问图像 URL 时发生的情况。 @MarcB

标签: php image png jpeg crop


【解决方案1】:

我回答了我自己的问题。 imagepng() 函数采用从 0 到 9 的质量参数 imagejpeg() 函数采用从 0 到 100 的质量参数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2011-11-16
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    相关资源
    最近更新 更多