Current location: Hot Scripts Forums » Programming Languages » PHP » imagecreatefromjpeg problem


imagecreatefromjpeg problem

Reply
  #1 (permalink)  
Old 05-15-09, 03:34 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
imagecreatefromjpeg problem

Hi there,

i got an image upload on my website where I want to control the file
size on my server. So I use imagecreatefromjpeg to create a smaller
image from the one that's uploaded..


Code:
function create_thumb($input2, $output2, $newwidth2 = 332, $newheight2 = 250) {
$bild2 = imagecreatefromjpeg($input2);
$x2 = imagesx($bild2);
$y2 = imagesy($bild2);
          
$thumb2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($thumb2, $bild2, 0, 0, 0, 0, $newwidth2, $newheight2, $x2, $y2);
imagejpeg($thumb2, $output2, 90);
}

// Path to the source image
$source2 = "../../images/$newname3";
	
// New image will be saved as:
$target2 = "../../images/$newname3aa";

$bild2 = imagecreatefromjpeg($source2);
$xx = imagesx($bild2);
$yy = imagesy($bild2);  
    
$xx2 = $xx / 2;
$yy2 = $yy / 2;

// Create it
create_thumb($source2, $target2, $xx2, $yy2);

This works for me when I upload images the are justx a bit oversized. But when I upload a 1,5 mb
pictuere I got this error:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 12288 bytes)

To me 1,5 mb is way smaller than the allowed memory site wich is 8mb (8388608/1024=8192 kb --> 8192/1024=8 mb right??)

Uploading the 1,5 mb image without the resize part is no problem at all.


Where does this go wrong?..


_j
Reply With Quote
  #2 (permalink)  
Old 05-15-09, 02:28 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
You must consider everything.

You don't have just the image in memory.

You also have the new image (up to 3mb now), plus all of your custom functions, and the current script that is running. Which is all of your variables, calculations, etc. I'm not really sure if the built in functions are held in this allocated memory or not, but I think they are.
Reply With Quote
  #3 (permalink)  
Old 05-15-09, 02:40 PM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
A 1.5MB image will consume a lot of memory when loaded with the gd library. There's a lot of additional stuff it does in memory.

You'll have to increase the memory allocated to PHP in php.ini.

The setting is: "memory_limit." Set it to something like: 16M or 32M if you can.

(The error is telling you it couldn't allocate the amount of memory needed above the amount already
in use. Yes, the amount noted in the error is less than your current memory limit.)

If your hosting account doesn't allow you to have custom php.ini setttings or you don't control the box, you'll have to find a different solution.

One possible solution is to detect if the file size is greater than some amount and if it is, pass the
image out to a different server with the requisite memory allocated to PHP - possibly a server
running at your home. Then send the image back to the hosting server after it's been processed
to a special URL that only your home server can access (password protection, etc.).

Obviously another solution is to get a different host.

Hope that helps.
Reply With Quote
  #4 (permalink)  
Old 05-18-09, 07:23 PM
Thyrosis's Avatar
Thyrosis Thyrosis is offline
Newbie Coder
 
Join Date: Dec 2008
Location: South UK
Posts: 66
Thanks: 2
Thanked 0 Times in 0 Posts
Another way is to limit the pixel size of the images uploaded.

The way GD library works the imagecreatefromjpeg function is like this:

pixel width of image * pixel height of image * 5 = memory consumption.

So, if you indeed have an 8mb memory limit, you probably have 8.000.000 bytes to play with.
Subtract 50.000 for other script functionality, leaves 7.950.000.
This means the maximum amount of pixels in your uploaded image is (7.950.000/5) = 1.590.000.
The maximum pixel resolution is then 1260x1260 (for a square one)

So, to solve the problem as it is, retrieve the height and width of the uploaded image, multiply them with each other and again with 5, see if it's less then 7.950.000 and continue with the script. If it's more then that 7.950.000, exit the script and let them resize the picture before continuing.

/edit: I've only just seen that it is actually 8MB, not 8MB as in 8.000.000 bytes. So yeah, adapt the numbers a bit, but the theory is still the same.

Good luck!
Reply With Quote
  #5 (permalink)  
Old 05-19-09, 04:00 AM
giriprasad.gn giriprasad.gn is offline
New Member
 
Join Date: Sep 2007
Location: india
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Dear jonnekke,

I think storing the image in the database is always not advisable because ur DB may bulge in size as you insert more than very few images.

It is rather easy to store them in a separate folder and just store the filename in the database and you can use these filenames whereever u want to display the images.

Reply With Quote
  #6 (permalink)  
Old 05-20-09, 04:50 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 706
Thanks: 0
Thanked 0 Times in 0 Posts
@giriprasad.gn: where did you find i'm storing in a DB?.. only the name of the picture is stored in a DB
not the whole image. That is stored in my image folder.

_j
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pls help website problem! iangarrinud CSS 1 01-03-08 01:48 PM
login, roles problem dbrook007 ASP.NET 10 11-10-06 03:42 PM
Form Display Problem neevrap02 Visual Basic 1 09-05-06 05:18 AM
Count problem kasic ASP.NET 1 10-20-04 12:23 AM
Asp and Microsoft Access 2002 problem gop373 ASP 2 10-06-04 09:13 AM


All times are GMT -5. The time now is 08:35 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.