2013-02-26 87 views
1

我目前正在使用一個php gd實現來調整內存不斷耗盡的圖像 - 相當快。我想這個問題是PHP的功能,如imagecreatefromstring等使用php生成圖像縮略圖 - 內存不足

是否有一個簡單的實施調整圖像大小不使用此功能,所以我不會增加我的php.ini內存限制?

+0

PHP函數也許有在你的代碼中的內存泄漏,但誰在乎:)(我們展示代碼) – AmazingDreams 2013-02-26 12:05:47

回答

1

GD不使用那麼多的內存,所以你在你的代碼中有其他問題。

如果您調整多個圖像的大小並且不在新創建的圖像上調用imagedestroy,則會在內存泄漏中運行。

+0

真彩色圖像可以用'寬×高×4×內存中的字節。 500萬像素圖像上的'createimagefromjpg'和20MB內存限制將立即耗盡所有內存。 – 2013-02-26 12:17:38

+0

我知道,但PHP5.3中的默認內存限制是128M,這肯定適合。如果您的主機不支持合適的內存限制,並且不會爲您更換它,則應該尋找其他主機。 – Philipp 2013-02-26 12:28:24

4

這裏是你

function make_thumb($src, $dest, $desired_width,$desired_h) { 

    /* read the source image */ 
    $source_image = imagecreatefromjpeg($src); 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 

    $desired_height = $desired_h; 

    /* create a new, "virtual" image */ 
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height); 

    /* copy source image at a resized size */ 
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); 

    /* create the physical thumbnail image to its destination */ 
    imagejpeg($virtual_image, $dest); 
} 
+1

即使您的版本稍有不同,您應該真正包含信用:https:// davidwalsh .name/create-image-thumbnail-php – Brett 2016-01-30 13:25:48

+0

@Brett嗯,你是對的,我真的不記得我之前從哪裏得到代碼,我從一個已經使用它的項目中複製它,我是不知道你鏈接的來源是否是來源,也許他從其他地方也可以得到它,以及我做過的myabe。如果我知道我會鏈接它的來源。 – ImadBakir 2016-06-01 13:23:57