`

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

    博客分类:
  • php
阅读更多

当图片超过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

<?php
// $img_base = base directory structure for thumbnail images
// $w_dst = maximum width of thumbnail
// $h_dst = maximum height of thumbnail
// $n_img = new thumbnail name
// $o_img = old thumbnail name
function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img){

ini_set('memory_limit', '100M'); // handle large images
unlink($img_base.$n_img); // remove old images if present
unlink($img_base.$o_img);
$new_img = $img_base.$n_img;

$file_src = $img_base."img.jpg"; // temporary safe image storage
unlink($file_src);
move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);

list($w_src, $h_src, $type) = getimagesize($file_src); // create new dimensions, keeping aspect ratio
$ratio = $w_src/$h_src;
if ($w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}

switch ($type){

case 1: // gif -> jpg
$img_src = imagecreatefromgif($file_src);
break;
case 2: // jpeg -> jpg
$img_src = imagecreatefromjpeg($file_src);
break;
case 3: // png -> jpg
$img_src = imagecreatefrompng($file_src);
break;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample

imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
imagejpeg($img_dst, $new_img); // save new image

unlink($file_src); // clean up image storage
imagedestroy($img_src);
imagedestroy($img_dst);
}

$p_id = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
convertPic("your/file/structure/", 150, 150, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");

?>

分享到:
评论

相关推荐

    自适应长宽生成缩略图PHP GD代码

    $source = imagecreatefromjpeg($filename); imagecopyresized($temppic, $source, 0, 0, $picx, $picy, $width, $height, $width, $height); 读取并生成图像 $newwidth=163; $newheight=166; 新图片尺寸 $thumb = ...

    在线生成图片 源码 含教程 详细注释 PSD分层文件

    在网站上GD库通常用来生成缩略图,或者用来给图片添加水印,或者用来生成验证码等。幸运的是,SAE默认是对GD库支持的。 本地开发php默认是不支持GD库的,需要自己开启,开启的方式是:在本地PHP安装目录下找到...

    php生成图片缩略图的方法

    本文实例讲述了php生成图片缩略图的方法。分享给大家供大家参考。具体如下: 这里需要用到GD2 library function make_thumb($src,$dest,$desired_width) { /* read the source image */ $source_image = ...

    PHP100视频教程 (三十五、PHP5文字图片混合水印与缩略图)

    2、ImageCreateFrom* 图片载入函数 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng imagecreatefromwbmp imagecreatefromstring 3、imagecopy 图片合并函数 4、ImageCopyResized图片剪切函数

    PHP100视频教程 35:PHP5文字图片混合水印与缩略图

    1、介绍PHP水印原理和流程2、ImageCreateFrom* 图片载入函数 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng imagecreatefromwbmp imagecreatefromstring3、imagecopy 图片合并函数4、...

    PHP100视频教程 35:PHP5文字图片混合水印与缩略图.rar

    2、ImageCreateFrom* 图片载入函数 ... imagecreatefromjpeg  imagecreatefrompng  imagecreatefromwbmp  imagecreatefromstring 3、imagecopy 图片合并函数 4、ImageCopyResized图片剪切函数

    php 缩略图实现函数代码

    array getimagesize ( string $filename [, array &$imageinfo ] ) 取得图像大小 resource imagecreatetruecolor ( int $x_size , int $y_size ) 新建一个真彩色图像 resource imagecreatefromjpeg ( string $...

    Defeating-PHP-GD-imagecreatefromjpeg:利用PHP-GD内置函数imagecreatefromjpeg()中的缺陷的概念验证

    利用PHP-GD imagecreatefromjpeg()函数利用PHP-GD内置函数imagecreatefromjpeg()中的缺陷的概念验证。 受到Reddit关于我先前关于利用imagecreatefromgif()PHP-GD函数的讨论的评论之一的启发。警告:此POC仅...

    如何实现php图片等比例缩放

    通过文章给出的源代码可实现针对图片的等比缩放生成缩略图的功能,非常实用的技巧哦。 新建文件index.php,需要在统计目录下有个图片为pic.jpg(可根据源码进行更改图片的名称) 源代码如下: &lt;?php $filename=...

    php下图片文字混合水印与缩略图实现代码

    一 imageCreateFrom* 图片载入函数 //针对不同的后缀名图片 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng imagecreatefromwbmp imagecreatefromstring 使用格式:imagecreatefromgif(“jjj.gif”);...

    浅谈关于PHP解决图片无损压缩的问题

    本文介绍了关于PHP解决图片无损压缩的问题,分享给大家,具体如下: 代码如下: header("Content-type: image/jpeg"); $file = "111.jpg"; $percent = 1.5; //图片压缩比 list($width, $height) = getimagesize($...

    PHP实现获取图片颜色值的方法

    本例讲述了PHP实现获取图片颜色值的方法,PHP获取图片颜色值检测图片主要颜色是通过imagecreatefromjpeg函数读取图片,再循环获得各个颜色值加以计算实现的。 具体代码如下: &lt;?php $i=imagecreatefromjpeg(...

    imagecreatefromjpeg、imagecreatefrompng、imagecreatefromgif绕过

    具体一句话可以打开来看一下

    php图片加水印

    *@param $srcImg 原图 *@param $waterImg 水印图片 *@param $savepath 保存路径 *@param $savename 保存名字 *@param $position 水印位置 *1:左上 2:右上 3:居中 4:左下 5:右下 *@param $opacity 透明度 ...

    php实现给图片加灰色半透明效果的方法

    3.使用 imagecopy()方法,把新创建的半透明图片与原图合并 具体实现代码如下: 复制代码 代码如下:&lt;?php /*php 给图片加灰色透明效果*/ $imfile = ‘./0.jpg’;//原图 $origim = imagecreatefromjpeg($imfile);...

    如何使用PHP给图片加水印

    为了防止辛苦做出来的图片被盗用,很多照片都会加上水印,可以直接用图片工具添加水印再上传,但PHP中就可以实现给图片加水印的功能,本文章向码农们介绍 php 给图片加水印的两种方法,感兴趣的码农可以参考一下本...

    PHP中图片等比缩放的实例

    php  //图片的等比缩放 //因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源 $src=imagecreatefromjpeg(‘a.jpg’); //取得源图片的宽度和高度 $size_src=getimagesize(‘a.jpg’); ...

    解决PHP上传非标准格式的图片pjpeg失败的方法

    前一阵子网站新上了相册功能,可最近总发现有一些用户上传的图片文件链接失效,代码检查了很多次,测试也做的比较充分了,但还是会出现上传失败的问题,很是郁闷,今天终于找到了解决办法。 从备份源中找到了用户...

    PHP实现浏览器中直接输出图片的方法示例

    但有时候我们需要把图片做一些处理,比如改一下图片质量或者尺寸再显示出来,我们可以使用PHP内置的图片处理函数imagecreatefromjpeg(或imagecreatefrompng)由文件或 URL 创建一个新图象和imagejpeg(或imagepng)...

    php绘图之加载外部图片的方法

    本文实例讲述了php绘图之加载外部图片的方法。分享给大家供大家参考。...$im_new = imagecreatefromjpeg(“baidu.jpg”);//返回图像标识符 $im_new_info = getimagesize(“baidu.jpg”);//取得图像大小,返回一个数

Global site tag (gtag.js) - Google Analytics