|
Web design and Programming Discuss website creation and other programming topics. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
|||
|
Another question
Hi there,
I wrote a PHP class to crop images using the gd library...works fine on my local server but it appears my host has a smaller memory limit...any ideas of how to work around it? It seems I cannot upload any images larger than 300k with this cropping class. This is the error I get: Quote:
Code:
<?php class ImageCropper { var $mediumWidth, $mediumHeight, $resizeWidth, $resizeHeight, $theImage, $width, $height, $newWidth, $newHeight, $imageType, $thumbSize, $error; function ImageCropper() { $this->mediumWidth = 275; $this->mediumHeight = 200; $this->resizeWidth = 325; $this->resizeHeight = 250; $this->error = null; } // Constructor function makeThumb($image, $iName, $iSize=110) // Thumbnails are square so there are only three params { $this->thumbSize = $iSize; list($this->width,$this->height, $this->imageType) = getimagesize($image); if($this->generateImageFromType($image)) { if ($this->width > $this->height) { $this->resizeWidth = $this->thumbSize + 100; $this->makeLandscape(); } else if ($this->height > $this->width) { $this->makePortrait(); $this->resizeHeight = $this->thumbSize + 100; } else { $this->resizeWidth = $this->thumbSize + 100; $this->makeLandscape(); } if ($this->width > $this->resizeWidth || $this->height > $this->resizeHeight) { if($this->imageType != IMAGETYPE_GIF) { $tempImage = imagecreatetruecolor( $this->newWidth, $this->newHeight ); $temp = imagecreatetruecolor( $this->thumbSize, $this->thumbSize); } else { $tempImage = imagecreate( $this->newWidth, $this->newHeight ); $temp = imagecreate( $this->thumbSize, $this->thumbSize); } imagecopyresampled( $tempImage, $this->theImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height ); imagecopy( $temp, $tempImage, 0, 0,($this->newWidth/2)-($this->thumbSize/2),($this->newHeight/2)-($this->thumbSize/2), $this->newWidth, $this->newHeight ); $this->renderImage($temp, $iName); imagedestroy( $temp ); imagedestroy( $tempImage ); } else { if($this->imageType != IMAGETYPE_GIF) { $temp = imagecreatetruecolor( $this->thumbSize, $this->thumbSize); } else { $tempImage = imagecreate( $this->newWidth, $this->newHeight ); $temp = imagecreate( $this->thumbSize, $this->thumbSize); } imagecopy( $temp, $this->theImage, ($this->thumbSize/2)-($this->width/2), ($this->thumbSize/2)-($this->height/2),0,0, $this->width, $this->height ); // resize to width $this->renderImage($temp, $iName); imagedestroy( $temp ); } } else echo $this->error; } // End Make Thumb function makeOtherSize($image, $iName, $iWidth=275, $iHeight=200) { $this->mediumWidth = $iWidth; $this->mediumHeight = $iHeight; $this->resizeWidth = $iWidth + 50; $this->resizeHeight = $iHeight + 50; list($this->width,$this->height, $this->imageType) = getimagesize($image); if($this->generateImageFromType($image)) { if ($this->width > $this->height) { $this->makeLandscape(); } else if ($this->height > $this->width) { $this->makePortrait(); } else { $this->makeLandscape(); } if ($this->width > $this->resizeWidth || $this->height > $this->resizeHeight) { if($this->imageType != IMAGETYPE_GIF) { $tempImage = imagecreatetruecolor( $this->newWidth, $this->newHeight ); $temp = imagecreatetruecolor( $this->mediumWidth, $this->mediumHeight); } else { $tempImage = imagecreate( $this->newWidth, $this->newHeight ); $temp = imagecreate( $this->mediumWidth, $this->mediumHeight); } imagecopyresampled( $tempImage, $this->theImage, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height ); imagecopy( $temp, $tempImage, 0, 0,($this->newWidth/2)-($this->mediumWidth/2),($this->newHeight/2)-($this->mediumHeight/2), $this->newWidth, $this->newHeight ); $this->renderImage($temp, $iName); imagedestroy( $temp ); imagedestroy( $tempImage ); } else { if($this->imageType != IMAGETYPE_GIF) { $temp = imagecreatetruecolor( $this->mediumWidth, $this->mediumHeight); } else { $temp = imagecreate( $this->mediumWidth, $this->mediumHeight); } imagecopy( $temp, $this->theImage, ($this->mediumWidth/2)-($this->width/2), ($this->mediumHeight/2)-($this->height/2),0,0, $this->width, $this->height ); // resize to width $this->renderImage($temp, $iName); imagedestroy( $temp ); imagedestroy( $tempImage ); } } else { echo $this->error; } } function generateImageFromType($toBeMade) { switch($this->imageType) { case IMAGETYPE_GIF: $this->theImage = imagecreatefromgif($toBeMade) or die("Error: Cannot find image!"); return true; break; case IMAGETYPE_JPEG: $this->theImage = imagecreatefromjpeg($toBeMade) or die("Error: Cannot find image!"); return true; break; case IMAGETYPE_PNG: $this->theImage = imagecreatefrompng($toBeMade) or die("Error: Cannot find image!"); return true; break; default: $this->error("This image type is not allowed:".$this->imageType); return false; break; } } function renderImage($theImage,$iName) { switch ($this->imageType) { case IMAGETYPE_JPEG: imagejpeg($theImage, $iName); break; case IMAGETYPE_GIF: imagegif($theImage, $iName); break; case IMAGETYPE_PNG: imagepng($theImage, $iName); break; default: break; } } function makeLandscape() { if ($this->width > $this->resizeWidth) { $ratio = $this->exceededAspectRatio(); if ($ratio == true) { $this->newHeight = $this->resizeHeight; $this->newWidth = ceil( $this->newHeight*($this->width/$this->height)); echo("got here"); } else { $this->newWidth = $this->resizeWidth; $this->newHeight = ceil( $this->newWidth*($this->height/$this->width)); } } else { $this->newWidth = $this->width; $this->newHeight = $this->height; } } // End Make LandScape function makePortrait() { if ($this->height > $this->resizeHeight) { $ratio = $this->exceededAspectRatio(); if ($ratio == true) { $this->newHeight = $this->resizeHeight; $this->newWidth = ceil( $this->newHeight*($this->width/$this->height)); echo("got here"); } else { $this->newHeight = $this->resizeHeight; $this->newWidth = ceil( $this->newHeight*($this->width/$this->height)); } } else { $this->newWidth = $this->width; $this->newHeight = $this->height; } } function exceededAspectRatio() { if ($this->width > $this->height) { if ($this->width / $this->height > 2) return true; } else { return false; } if ($this->height > $this->width) { if ($this->height / $this->width > 2) return true; } else { return false; } } } ?>
__________________
|
#2
|
||
|
Actually around 30 MB is the server limit. I presume you have shared hosting?
You can try to add this to a .htaccess file in your root directory. php_value memory_limit 40M Note that this will only work if the server admin allows it. |
#3
|
||
|
Forgot one thing, try to gather how much memory is used before the script has done it's magic and after with memory_get_usage(). 30MB is ALOT for a script especially if it's handling a file of only 300 KB.
Don't have alot of experience with shared hosting but I do recall that it's not always that liked when you change the limit as more server memory will be used which they can use for another customer. So might be good to ask first if you can increase it but firstly look over your code so that you close all opened files and so on after your done with it.I'm sure scott or Ici can help you with the code, way to much for my dyslexic arse to read heh Last edited by SilentTrigger; 01-03-2009 at 01:10 PM. |
#4
|
||
|
Ok the .htaccess worked! Cuz you rocketh ST.
Now... I will do the mem usage thing. Any idea as to why I am getting that other warning? I don't think I am using globals at all in that script.
__________________
|
#5
|
||
|
Actually not sure why you're getting that error, searched your script for obvious things but didn't see any declaration of any globals. But then as noted above I haven't read it though because thats just too much to read. I hate to read I'm afraid, need to be a small amount
The most I've read is 10 pages in a book and that was like klimbing mount everest mentaly. Well besides the code I write on the Robots but as it's me writing it there's not an issue hehe |
#6
|
||
|
OK well the memory usage:
Beginning of makeThumb:2793000 End of makeThumb:18579868 Beginning of makeOtherSize:18579868 End of makeOtherSize:18579976 My guess is that the page I am using is keeping the temp file in memory... something else in makeThumb isn't right either.
__________________
|
#7
|
||
|
memory_limit in php should be script based and not serverside. Looks like it isn't :/
Did you take a reading before you do anything? Shouldn't use much memory at all if you read it in the beginning of the script before you do anything |
#8
|
||
|
PHP Code:
|
#9
|
||
|
Just figured it out..with the memory deal....the other stuff with globals still confuses me.
imagedestroy( $this->theImage );
__________________
|
#10
|
||
|
So what was it then?
|
#11
|
||
|
imagedestroy( $this->theImage );
You were right too, I had just fixed that as well. I was forgetting to get rid of the first image when it was created.
__________________
|
#12
|
||
|
Ah that explain it, no wonder I didn't find that when searching for imagedestory when it's not there hehe
|
#13
|
||
|
Beginning of make thumb:2802320
End of make thumb:2816216 Beginning of make other size:2816216 End of make other size:2816328 Now I just need to figure out how to kill the temp file that PHP stores for the image in the files array.
__________________
|
#14
|
||
|
Not that the other one was either but there was an imagedestroy above it.
Sorry for not actually reading the code, bit pathetic, but it's a mental block, as I had trubles reading as a kid I developed a hate for it and it continues still today hehe How do you gather the image that you upload? POST? Create a temporary file? Have you closed the temporary file? Guess this is not the way you do it. |
#15
|
||
|
I uploaded the file and saved it to the server then I used the server file in this script....thing is that the upload.php file is what instantiates the imagecropper class....so technically memory is still used for that temp file.
__________________
|
#16
|
||
|
And you let the upload.php file close the tmp file when it's done with it right?
|
#17
|
||
|
Is the image by any chance around 5-6 mb in size?
|
#18
|
||
|
I am not sure how to do it. From my understanding PHP is supposed to kill any post data when the browser changes pages. The file I uploaded was 1.3 mb
__________________
|
#19
|
||
|
Silent Trigger gets the king and winner award of the month
__________________
|
#20
|
||
|
Never heared that (defenetly doesn't mean it's false) but have you tried to close the file/flush the temp file?
Heh, why? Haven't done anything lol |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Question | ƒalcon | Joint Operations | 3 | 10-12-2006 04:45 PM |
just a question | Lucky | Feedback / Novahq.net Support | 3 | 07-29-2005 11:10 AM |
Question | SMITIE | Sigs and Graphics | 8 | 07-18-2005 07:20 PM |
Question :D | V99 | Delta Force | 7 | 09-10-2003 12:23 AM |
i have a question | †W¡§†ëÐ «M§§» | Hardware and Software | 5 | 08-23-2003 09:39 AM |