从网址创建缩略图?

时间:2011-02-11 12:35:50

标签: php url external thumbnails

我正在尝试使用此功能从亚马逊s3的外部网址创建缩略图。

function resizeImage($originalImage,$toWidth,$toHeight){

    // Get the original geometry and calculate scales
    list($width, $height) = file_get_contents($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;

    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}

我遇到的问题是它似乎只与相对网址一起使用时出现以下错误。

Warning: Division by zero in /home/isd/public_html/swfupload/resize.php on line 15

Warning: Division by zero in /home/isd/public_html/swfupload/resize.php on line 16

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/isd/public_html/swfupload/resize.php on line 20

Warning: imagecreatefromjpeg(Chrysanthemum.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/isd/public_html/swfupload/resize.php on line 21

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/isd/public_html/swfupload/resize.php on line 22

Warning: Division by zero in /home/isd/public_html/swfupload/resize.php on line 15

Warning: Division by zero in /home/isd/public_html/swfupload/resize.php on line 16

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/isd/public_html/swfupload/resize.php on line 20

Warning: imagecreatefromjpeg(Desert.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/isd/public_html/swfupload/resize.php on line 21

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/isd/public_html/swfupload/resize.php on line 22

Warning: file_get_contents(http://isdprogress.s3.amazonaws.com/HQ preview.jpg) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported in /home/isd/public_html/swfupload/resize.php on line 5

现在有没有人可以使用的方法或功能可以将图像从外部网址调整为thunmbnails ???

由于

2 个答案:

答案 0 :(得分:2)

file_get_contents不返回图像资源的宽度和高度。

它将文件的内容返回到字符串:http://hu2.php.net/manual/en/function.file-get-contents.php

使用imagesx和imagesy代替:

http://hu2.php.net/manual/en/function.imagesx.php

http://hu2.php.net/manual/en/function.imagesy.php

添加

我不知道你的过程,但我认为你只是将图像作为字符串检索到file_get_contents,这不是一个有效的图像资源。

因此,您必须将此数据转换为图像资源。使用imagecreatefromstring函数:http://hu2.php.net/manual/en/function.imagecreatefromstring.php

免责声明:我没有看到您的完整代码,因此您只是猜测您没有图片资源:)

答案 1 :(得分:0)

确定

最后让它在这里为这个伟大的解决方案工作http://joedesigns.com/v22/?page=scripts_widgets&id=67

工作待遇;)