The imagecreatefromjpeg memory crashes when a thumbnail is generated for a large image.

Source: Internet
Author: User

Introduction: This is a detailed page for imagecreatefromjpeg memory crashes caused by thumbnails generated by large images. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 330430 'rolling = 'no'>

When the image size exceeds 1 MB, the following error may occur. Of course, this is also related to your php. ini settings. If memory_limit 16 Mb is too small in your PHP settings, the following error will occur!

Fatal Error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 3456 bytes) in

Solution

Ini_set ("memory_limit", "60 m ");

Dynamically set the size before imagecreatefromjpeg to solve the problem of insufficient memory.

Some servers may limit the use of ini_set () for this function, so that no error is reported or thumbnails cannot be generated.

Therefore, you only need to contact the server to manually modify PHP. ini.

References

In PHP, imagecreatefromjpeg creates a bitmap data in the memory to operate the image,
This requires a lot of content. A 24-bit image with a length and width of 2000 must be at least 2000X2000 X (24/8) = 12 m

/**************** The formula for calculating the size does not know whether the formula is accurate ************* ***************/

/**************** Here, there is also a function for converting images during upload written by foreigners. For details, refer ******* ***************/

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 specified 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. incluadditional features cocould 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 adjust tively load images (of more than 2 MB) 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 varous sing mispieces of information successfully load images of more than 2 MB and then create thumbnails. this participating ular 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 callthis 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 16 m
Php_value upload_max_filesize 6 m

<? 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 ");

?>

More articles about "generating thumbnails of large images causes imagecreatefromjpeg memory to crash"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/330430.html pageno: 11.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.