2012-11-19 16 views
1

我使用SimpleImage.php類:http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/來調整,壓縮和保存圖像。如何將圖片從url添加到temp?

通常用它與輸入的圖像:

$tmp_dir = $_FILES['file']['tmp_name']; 
$file_name = 'something.jpg'; 

include('SimpleImage.php'); 
$image = new SimpleImage(); 
$image->load($tmp_dir); 

$image->resizeToWidth($width); 
$image->save('imgd/l'.$file_name); 

但我怎麼能與圖像處理的URL僅僅?(從另一個網站)

$img = file_get_contents($url); 

上面$img變量保持的實際圖像。 那麼如何將它保存到臨時使用它呢? 如果這是正確的方法。

如果可能不需要更改SimpleImage.php類。

回答

2

隨着tempnam()

創建一個具有唯一文件名的文件時,設置爲 0600,在指定的目錄訪問權限。如果目錄不存在, tempnam()可能會在系統的臨時目錄中生成一個文件,並且 會返回該文件的名稱。

<?php 
$tmpfname = tempnam("/tmp", "UL_IMAGE"); 
$img = file_get_contents($url); 
file_put_contents($tmpfname, $img); 

include('SimpleImage.php'); 
$image = new SimpleImage(); 
$image->load($tmpfname); 

$image->resizeToWidth($width); 
$image->save('imgd/l'.$file_name); 
?> 
+0

感謝,現在我想通了,它的工作原理還剛剛與網址,我有另一個錯誤。 – slownage