2013-12-14 32 views
0

我有一個形象,我要像 (縮略圖,型材,小例如)創建不同大小錯誤的imagecreatefromjpeg

我的腳本(這部分..)

$new_img = @imagecreatetruecolor($new_width, $new_height); 
     switch (strtolower(substr(strrchr($file_name, '.'), 1))) { 
      case 'jpg': 
      case 'jpeg': 
       $src_img = @imagecreatefromjpeg($file_path); 

      /* See if it failed */ 
      if(!$src_img) 
      { 
       /* Create a black image */ 
       $src_img = imagecreatetruecolor(150, 30); 
       $bgc = imagecolorallocate($src_img, 255, 255, 255); 
       $tc = imagecolorallocate($src_img, 0, 0, 0); 

       imagefilledrectangle($src_img, 0, 0, 150, 30, $bgc); 

       /* Output an error message */ 
       imagestring($src_img, 1, 5, 5, 'Error loading ' . $file_path, $tc); 
      } 
     ...more cases... 

      default: 
      $src_img = null; 
     } 

當我調整爲型材,小我沒有當調整錯誤縮略圖我有這個錯誤

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: 

,並創建替換圖像中

由於

+1

附加調試是爲了。 '$ file_path'的內容是什麼?文件是否存在?它是你期望它的形象嗎?它實際上是一個jpeg嗎?刪除'@'錯誤抑制器。也許有一個更早的錯誤,你沒有看到因爲這些? –

+0

謝謝@MichaelBerkowski,是的文件存在(我可以創建其他維度),是的是jpg ...如果我刪除'@'我有這個錯誤'警告:imagecreatefromjpeg():gd-jpeg:JPEG庫報告不可恢復的錯誤:.... '$ file_path是我的圖像的絕對路徑,如 '/ uploads/attachments/folder/originals/image..jpg' – Barno

+0

@MichaelBerkowski png to jpg的錯誤謝謝! – Barno

回答

1
switch(strtolower($_FILES['fileupload']['type'])) 
         { 
          case 'image/jpeg': 
           $filename = imagecreatefromjpeg('imagepath/'.$post['fileupload']['name']); 
           break; 
          case 'image/png': 
           $filename = imagecreatefrompng('imagepath/'.$post['fileupload']['name']); 
           break; 
          case 'image/gif': 
           $filename = imagecreatefromgif('imagepath/'.$post['fileupload']['name']); 
           break; 
          default: 
           exit('Unsupported type: '.$_FILES['fileupload']['type']); 
         } 

         ob_start(); 
         imagejpeg($filename); 
         // large image 
         $large = base64_encode(ob_get_contents()); // returns output 

         $mainimgWidth = imagesx($filename); 
         $mainimgHeight = imagesy($filename); 

         $thumbWidth = intval($mainimgWidth/4); 
         $thumbHeight = intval($mainimgHeight/4); 
         $new = imagecreatetruecolor($thumbWidth, $thumbHeight); 
         $backgroundColor = imagecolorallocate($new, 255, 255, 255); 
         imagefill($new, 0, 0, $backgroundColor); 
         imagecopyresampled($new, $filename, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainimgWidth, $mainimgHeight); 

         /** Catch the imagedata */ 
         ob_start(); 

         imagejpeg($new); 



         $data = ob_get_clean(); 

         // Destroy resources 
         imagedestroy($filename); 
         imagedestroy($new); 

         // Set new content-type and status code 
         $thumb = base64_encode($data); 

TRY LIKE THIS

+0

我必須使用$ data ='ob_get_clean();'?我嘗試使用之前 '$ src_img = @imagecreatefromjpeg($ file_path);'但我有同樣的錯誤 – Barno

+0

捕捉到拇指圖像數據 –

+0

我從png更改爲jpg腳本不正確...有趣的反正謝謝: ) – Barno

相關問題