【问题标题】:(PHP) Image Resize failed(PHP) 图像调整大小失败
【发布时间】:2016-04-24 00:56:27
【问题描述】:

我尝试调整图像大小,但仍然出现同样的错误:

警告:imagecreatefromjpeg():gd-jpeg:JPEG 库报告不可恢复的错误:在第 18 行的 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 中

警告:imagecreatefromjpeg(): 'img/test/Bildschirmfoto 2014-01-25 um 08.05.13 nachm Kopie.jpg' 不是 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 中的有效 JPEG 文件第 18 行

警告:imagesx() 期望参数 1 是资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 19 行给出

警告:imagesy() 期望参数 1 是资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 20 行给出

注意:在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 24 行中遇到格式不正确的数值

注意:在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 24 行中遇到格式不正确的数值

警告:imagesx() 期望参数 1 是资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 25 行给出

警告:imagesy() 期望参数 1 是资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 25 行给出

警告:imagecopyresized() 期望参数 2 是资源,布尔值在 /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php 第 25 行给出

所以,这是我的代码:

function make_thumb($image_path, $thumb_path, $thumb_width) { 
    $src_img = imagecreatefromjpeg("$image_path"); 
    $origw=imagesx($src_img); 
    $origh=imagesy($src_img); 
    $new_w = $thumb_width; 
    $diff=$origw/$new_w; 
    $new_h=$new_w; 
    $dst_img = imagecreatetruecolor($new_w,$new_h); 
    imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); 
    imagejpeg($dst_img, "$thumb_path"); 
    RETURN TRUE; 
}

【问题讨论】:

  • 'img/test/Bildschirmfoto 2014-01-25 um 08.05.13 nachm Kopie.jpg' 不是有效的 JPEG 文件
  • 哦,好吧...那是什么?

标签: php image resize


【解决方案1】:

根据您的错误,资源不是有效的 jpg 作为错误状态,之后的以下错误只是一个链。

我建议你使用我在PHP image upload, resize and crop called eImage 上写的这个库

试试这个代码:

function make_thumb($image_path, $thumb_path, $thumb_width) {
    $src_img = imagecreatefromjpeg("$image_path");
    $origw=imagesx($src_img);
    $origh=imagesy($src_img);

    // This calculations goes is for?
    // note that diff is not being used
    $new_w = $thumb_width;
    $diff = $origw/$new_w;
    $new_h = $new_w;
    // end of calculation
    $canvas = imagecreatetruecolor($new_w,$new_h);
    imagecopyresampled($canvas,$src_img,0,0,0,0,$new_w,$new_h,$origw,$origh);
    imagejpeg($canvas, $thumb_path);
    imagedestroy($src_img);
    imagedestroy($canvas);
    return true;
}

【讨论】:

  • 谢谢!这对我有帮助
  • 不要忘记将答案标记为正确答案。很高兴知道它有效:)
猜你喜欢
  • 2017-02-04
  • 2011-11-25
  • 2012-01-09
  • 1970-01-01
  • 2011-03-31
  • 2011-08-30
  • 2011-02-18
  • 1970-01-01
  • 2014-04-10
相关资源
最近更新 更多