dsyo9700 2016-04-24 00:56
浏览 83
已采纳

(PHP)图像调整大小失败:/

I try to resize an image, but I still get the same error:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 18

Warning: imagecreatefromjpeg(): 'img/test/Bildschirmfoto 2014-01-25 um 08.05.13 nachm Kopie.jpg' is not a valid JPEG file in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 18

Warning: imagesx() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 19

Warning: imagesy() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 20

Notice: A non well formed numeric value encountered in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 24

Notice: A non well formed numeric value encountered in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 24

Warning: imagesx() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 25

Warning: imagesy() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 25

Warning: imagecopyresized() expects parameter 2 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/dad/gallery.php on line 25

So, here's my code:

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; 
}

And here's the picture:

1

  • 写回答

1条回答 默认 最新

  • dps43378 2016-04-24 02:19
    关注

    From your error the resource is not an valid jpg as the error state, and the following errors after that just a chain.

    I would suggest you to use this Library I wrote on PHP image upload, resize and crop called eImage

    Try this code Instead:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启