2011-04-08 88 views
6

我試圖從無法控制的外部網站加載圖像。PHP imagecreatefromstring():gd-jpeg,libjpeg:可恢復的錯誤

大部分時間它工作正常(迄今測試數百個圖像)。

它現在給我這個錯誤對一個特定的形象:

imagecreatefromstring():GD-JPEG,libjpeg的:恢復的錯誤:腐敗JPEG數據:數據段

過早結束此行:

$im = @imagecreatefromstring($imageString); 

我讀到這一步,說明添加的建議:

ini_set("gd.jpeg_ignore_warning", true); 

但這沒有效果,我仍然收到錯誤。在通話之前我正在做ini_set。這是相關的嗎?

我真的被困在如何忽略這個錯誤並繼續。

+1

是否有權限設置此參數INI?這個警告是否會導致腳本終止? – SimonDowdles 2011-04-08 08:38:23

+0

順便說一句,你只是想顯示這個圖像,或者你真的想將它複製到你的服務器? – SimonDowdles 2011-04-08 08:39:09

+0

我沒有收到有關INI文件設置的任何警告,它仍然是gdcreate函數的致命錯誤。 – Dave 2011-04-08 08:52:23

回答

6

這個問題是由於我的錯誤處理。我會設置一個錯誤處理程序,所以我打電話給

$im = @imagecreatefromstring($imageString); 

沒有抑制錯誤。

通過修改我的錯誤處理程序有:

if (error_reporting() === 0) 
    { 
     // This copes with @ being used to suppress errors 
     // continue script execution, skipping standard PHP error handler 
     return false; 
    } 

我現在可以正確地抑制選擇錯誤。

我發現的信息在這裏:http://anvilstudios.co.za/blog/php/how-to-ignore-errors-in-a-custom-php-error-handler/

+0

我一直在撞我的頭撞牆這些錯誤超過了數年,並且無法使用@來壓制它們,我從來沒有想過這將是我們的自定義錯誤處理程序,它將是罪魁禍首,所以感謝發佈該鏈接並回答! – oucil 2015-07-30 21:31:49

0

如果你只是顯示的圖像,然後我建議只是閱讀的內容,並顯示相關圖片如下:

$img = "http://path/to/image"; 
$contents = file_get_contents($img); 
header("Content-Type: image/jpeg"); 
print($contents); 

如果您想將圖像複製到你的服務器,你有幾種選擇,其中兩個是copy()功能或上面所用的方法,然後fwrite()

選項1 - 的copy()功能,可從PHP 4

$file1 = "http://path/to/file"; 
$dest = "/path/to/yourserver/lcoation"; 
$docopy = copy($file1, $dest); 

選項2 - 使用file_get_contents()fwrite()

$img = "http://path/to/image"; 
$contents = file_get_contents($img); 
$newfile = "path/to/file.ext"; 
$fhandler = fopen($newfile, 'w+'); //create if not exists, truncate to 0 length 
$write = fwrite($fhandler, $contents); //write image data 
$close = fclose($fhandler); //close stream 
chmod(0755, $newfile); //make publically readable if you want 

我希望你能找到在上面

+0

如果您最終使用'file_get_contents()'方法以及'fwrite()',則需要在遠程文件上實現自己的擴展檢查。如果您使用'file_get_contents()'來僅顯示圖像,則需要驗證圖像擴展名並將相對內容類型應用於標題。 – SimonDowdles 2011-04-08 08:51:10

0

一些使用考慮,你想使一個縮略圖並保存它,你可以實現方便的調整大小功能,如下所示:

<?php 
function resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality){ 

    $ext1 = explode(".",trim($sourcefile)); 
    $ext = strtolower(trim(array_slice($sext1,-1))); 

    switch($ext): 
     case 'jpg' or 'jpeg': 
      $img = imagecreatefromjpeg($sourcefile); 
     break; 
     case 'gif': 
      $img = imagecreatefromgif($sourcefile); 
     break; 
     case 'png': 
      $img = imagecreatefrompng($sourcefile); 
     break; 
    endswitch; 

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

    if ($width > $height) { 
     $newwidth = $thumbwidth; 
     $divisor = $width/$thumbwidth; 
     $newheight = floor($height/$divisor); 
    } 
    else { 
     $newheight = $thumbheight; 
     $divisor = $height/$thumbheight; 
     $newwidth = floor($width/$divisor); 
    } 

    // Create a new temporary image. 
    $tmpimg = imagecreatetruecolor($newwidth, $newheight); 

    // Copy and resize old image into new image. 
    imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 

    // Save thumbnail into a file. 

    switch($ext): 
     case 'jpg' or 'jpeg': 
      $makeimg = imagejpeg($tmpimg, $endfile, $quality); 
     break; 
     case 'gif': 
      $makeimg = imagegif($tmpimg, $endfile, $quality); 
     break; 
     case 'png': 
      $makeimg = imagepng($tmpimg, $endfile, $quality); 
     break; 
    endswitch; 

    // release the memory 
    imagedestroy($tmpimg); 
    imagedestroy($img); 

     if($makeimg){ 
     chmod($endfile,0755); 
     return true; 
     }else{ 
     return false; 
     } 
    } 
?> 

然後,複製完t後他要的文件在我的答案高於此使用我的方法之一,你的服務器,你可以簡單地套用功能如下:

$doresize = resize($sourcefile, $endfile, $thumbwidth, $thumbheight, $quality); 
echo ($doresize == true ? "IT WORKED" : "IT FAILED"); 

此功能提供了我很好。我每天將它應用於1000張圖像,並且它像魅力一樣運作。

+0

代碼是有點兒車 - 在移動到別的東西之前修復了一個本地副本上的幾個錯別字 - 看起來像一個有用的功能只是不是最好的複製/粘貼/測試 – Alvin 2012-10-26 19:20:02

2

這可以與解決:

ini_set ('gd.jpeg_ignore_warning', 1);