开发者

Create thunmbnail from urls?

i am trying to use this function to create thumbnails from extenals urls from amazon 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;
}

The problem i had is it only seems to work with relative urls i get the following errors.

Warning: Division by zero in /home/isd/public_h开发者_高级运维tml/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

Does anyone now a good way or function i can use that works to resize images to thunmbnails from external urls???

Thanks


file_get_contents does not return the width nor the height of an image resource.

It returns the content of a file into a string: http://hu2.php.net/manual/en/function.file-get-contents.php

Use imagesx and imagesy instead:

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

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

Add:

I don't know your process, but I think you just retrieve the image as a string with file_get_contents, which is not a valid image resource.

So, you have to convert this data into an image resource. Use imagecreatefromstring function: http://hu2.php.net/manual/en/function.imagecreatefromstring.php

Disclaimer: I don't see your full code, so it's just a guess that you don't have an image resource :)


Ok

Finally got it working for this great solution here http://joedesigns.com/v22/?page=scripts_widgets&id=67

Work a treat ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜