簡體   English   中英

PHP imagecreatefromstring():gd-jpeg,libjpeg:可恢復的錯誤

[英]PHP imagecreatefromstring(): gd-jpeg, libjpeg: recoverable error

我正在嘗試從我無法控制的外部網站加載圖像。

大部分時間它工作正常(到目前為止測試了數百個圖像)。

它現在給了我一個特定圖像的錯誤:

imagecreatefromstring():gd-jpeg,libjpeg:可恢復的錯誤:損壞的JPEG數據:數據段的過早結束

從這一行:

$im = @imagecreatefromstring( $imageString );

到目前為止我讀到的建議建議添加:

ini_set("gd.jpeg_ignore_warning", true);

但那沒有效果,我仍然得到錯誤。 我正在打電話之前做ini_set。 這有關系嗎?

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

問題是由於我的錯誤處理。 我設置了一個錯誤處理程序,所以我的調用

$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/

這可以解決:

ini_set ('gd.jpeg_ignore_warning', 1);

如果您只是顯示圖像,那么我建議只需閱讀內容並顯示如下圖像:

$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

我希望你能在上面找到一些用處

考慮到您想制作縮略圖並保存它,您可以實現一個方便的調整大小功能,如下所示:

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

然后,在我上面的答案中使用我的一種方法將文件復制到服務器之后,您可以簡單地應用如下函數:

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

這個功能很適合我。 我每天將它應用於1000張圖像,它就像一個魅力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM