2011-04-08 85 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);