凌的博客

您现在的位置是:首页  > 学无止境 > PHP

PHP

大图片生成缩略图 导致imagecreatefromjpeg 内存崩溃问题

2015-06-17PHP0
当图片超过1M时就可能出现以下错误  当然这个也跟你php.ini设置有关 如果你php设置里 memory_limit 16M 这个过小的话就会出现下面这个错误! Fatal error: Allowed memory size of 8388608 bytes exhausted (t
当图片超过1M时就可能出现以下错误  当然这个也跟你php.ini设置有关 如果你php设置里 memory_limit 16M 这个过小的话就会出现下面这个错误!
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3456 bytes) in
 
解决方法
ini_set("memory_limit", "60M"); 
在 imagecreatefromjpeg 前动态设置大小 以解决内存不足问题
有的服务器可能限制了这个函数的使用 ini_set()     这样的话就会既不报错 也无法生成缩略图  
所以只有联系服务器那边手动把php.ini修改一下 

相关参考
php 中,imagecreatefromjpeg 将在内存中创建一个位图数据来操作图片,
这需要大量的内容。一个长宽各为2000的24位图片,至少需要 2000 x 2000 x (24/8) = 12M的
/*****************     这个计算大小的公式不知道是否准确   ****************************/

/*****************     这里还有老外写的一个 上传时转换图片的函数 可以参考下  **********************/


Last night I posted the following note under move_upload_file and looked tonight to see if it got into the system. I happen to also pull up imagecreatefromjpeg and got reminded of many resize scripts in this section. Unfortunately, my experience was not covered by most of the examples below because each of them assumed less than obvious requirements of the server and browser. So here is the post again under this section to help others uncover the mystery in file uploading and resizing arbitrary sized images. I have been testing for several days with hundreds of various size images and it seems to work well. Many additional features could be added such as transparency, alpha blending, camera specific knowledge, more error checking. Best of luck.

--- from move_upload_file post ---

I have for a couple of years been stymed to understand how to effectively load images (of more than 2MB) and then create thumbnails. My note below on general file uploading was an early hint of some of the system default limitations and I have recently discovered the final limit I offer this as an example of the various missing pieces of information to successfully load images of more than 2MB and then create thumbnails. This particular example assumes a picture of a user is being uploaded and because of browser caching needs a unique number at the end to make the browser load a new picture for review at the time of upload. The overall calling program I am using is a Flex based application which calls this php file to upload user thumbnails.

The secret sauce is:

1. adjust server memory size, file upload size, and post size
2. convert image to standard formate (in this case jpg) and scale

The server may be adjusted with the .htaccess file or inline code. This example has an .htaccess file with file upload size and post size and then inline code for dynamic system memory.

htaccess file:
php_value post_max_size 16M
php_value upload_max_filesize 6M

如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论