gd-jpeg: JPEG library reports unrecoverable error 解决办法

 

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in C:\AppServ\www\mercPhoto.php on line 90
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'comp_logo/200803171023127332.jpg' is not a valid JPEG file in 
Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\AppServ\www\mercPhoto.php on line 100

 

解决见代码。

        /**
               *                等比例压缩图片
               * @param String $src_imagename 源文件名        比如 “source.jpg”
              * @param int    $maxwidth      压缩后最大宽度
              * @param int    $maxheight     压缩后最大高度
              * @param String $savename      保存的文件名    “d:save”
              * @param String $filetype      保存文件的格式 比如 ”.jpg“
              * @version 1.0
               */
         function resizeImage($src_imagename,$maxwidth,$maxheight,$savename,$filetype)
         {
              
              $typeArr=explode(".",$filetype);
             $type=$typeArr[1];
             switch($type)
             {
                case "png":
                $im=imagecreatefrompng($src_imagename);
                break;
                
                case "jpeg":
                $im=imagecreatefromjpeg($src_imagename);
                break;
                
                case "gif":
                $im=imagecreatefromgif($src_imagename);
                break;
             }

 
             $current_width = imagesx($im);
             $current_height = imagesy($im);
          
             if(($maxwidth && $current_width > $maxwidth) || ($maxheight && $current_height > $maxheight))
             {
                 if($maxwidth && $current_width>$maxwidth)
                 {
                     $widthratio = $maxwidth/$current_width;
                     $resizewidth_tag = true;
                 }
          
                 if($maxheight && $current_height>$maxheight)
                 {
                     $heightratio = $maxheight/$current_height;
                     $resizeheight_tag = true;
                 }
          
                 if($resizewidth_tag && $resizeheight_tag)
                 {
                     if($widthratio<$heightratio)
                         $ratio = $widthratio;
                     else
                         $ratio = $heightratio;
                 }
          
                 if($resizewidth_tag && !$resizeheight_tag)

                     $ratio = $widthratio;
                 if($resizeheight_tag && !$resizewidth_tag)
                     $ratio = $heightratio;
          
                 $newwidth = $current_width * $ratio;
                 $newheight = $current_height * $ratio;
          
                 if(function_exists("imagecopyresampled"))
                 {
                     $newim = imagecreatetruecolor($newwidth,$newheight);
                        imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$current_width,$current_height);
                 }
                 else
                 {
                     $newim = imagecreate($newwidth,$newheight);
                    imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$current_width,$current_height);
                 }
          
                 $savename = $savename.$filetype;
                 imagejpeg($newim,$savename);
                 imagedestroy($newim);
             }
             else
             {
                 $savename = $savename.$filetype;
                 imagejpeg($im,$savename);
             }  
die;
                   
        }

 

 

 

posted @ 2014-11-13 20:09  jshaibozhong  阅读(3306)  评论(0编辑  收藏  举报