尝试使用此功能从Jpg或Png文件创建缩略图时,我遇到了一个非常令人沮丧的问题。
有时它会起作用,有时却不起作用。当它不起作用时,我收到以下错误:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8192 bytes) in * on line 34
通过在线阅读,我认为我必须有内存泄漏,但我不确定是什么导致它。
这是功能:
function make_thumb($src, $dest, $desired_width) {
$fileType = pathinfo($src, PATHINFO_EXTENSION);
echo $fileType;
if ($fileType == 'JPG' || $fileType == 'JPEG' || $fileType == 'jpg' || $fileType == 'jpeg') {
$source_image = imagecreatefromjpeg($src);
} else if ($fileType == 'PNG' || $fileType == 'png') {
$source_image = imagecreatefrompng($src);
} else {
return false;
}
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagejpeg($virtual_image, $dest);
return true;
}
谢谢!
答案 0 :(得分:0)
将代码放在代码顶部:
ini_set("memory_limit","256M");