2011-02-11 104 views
0

我想用這個函數從亞馬遜s3的extenals url創建縮略圖。從網址創建thunmbnail?

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

我遇到的問題是,它似乎只與相對url的工作,我得到以下錯誤。

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 

有誰現在的好方法或功能,我可以使用效果來調整圖像從外部URL thunmbnails ???

謝謝

+0

什麼是resize.php 15行? – Select0r 2011-02-11 12:38:18

回答

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

免責聲明:我沒有看到你完整的代碼,所以它只是一個猜測,你沒有一個圖像資源:)

+0

嗨對不起,應該提及我使用getimagesize(),而不是file_get_contents,但仍有錯誤 – iSimpleDesign 2011-02-11 13:10:43