从 url 创建缩略图?
我正在尝试使用此功能从亚马逊 s3 的外部 URL 创建缩略图。
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
现在有人可以使用一种好方法或功能来将图像大小调整为外部网址的缩略图吗???
谢谢
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_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
Does anyone now a good way or function i can use that works to resize images to thunmbnails from external urls???
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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
免责声明:我没有看到您的完整代码,所以这只是猜测您没有图像资源:)
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 :)
好的
终于让它在这个伟大的解决方案中工作 http://joedesigns.com/v22/ ?page=scripts_widgets&id=67
好好享受一下;)
Ok
Finally got it working for this great solution here http://joedesigns.com/v22/?page=scripts_widgets&id=67
Work a treat ;)