Stephen68 Posted April 1, 2009 Share Posted April 1, 2009 Ok I have a script that will upload images for me, what I'm trying to do now is resize one of the images in that directory. I found a function that will do this for me but when I use it a get this error. Fatal error: Out of memory (allocated 33030144) (tried to allocate 860 bytes) in /homepages/41/d175892888/htdocs/admin/class.image-resize.php on line 56 Now if I had enough memory to upload the photo shouldn't I have enough to resize it? any help would be great, thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/ Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Post your code around line 56. It sounds like you are in an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798726 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Ok here is line 48 to 61 it's a function but I don't see a loop function image_resize() { set_time_limit(120); $this->get_mime(); $this->create_image(); $this->width = imagesx($this->image); $this->height = imagesy($this->image); $this->set_dimension(); $image_resized = imagecreatetruecolor($this->new_width,$this->new_height); imagecopyresampled($image_resized, $this->image, 0, 0, 0, 0, $this->new_width, $this->new_height,$this->width, $this->height); imagejpeg($image_resized,$this->path); } Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798827 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Out of curiosity, what is the size of the image? If it is close to 8 or 16m you may need to use ini_set and set the memory_limit for this script to be a bit more, as it does re-render the image at least twice, which would double the memory usage. Let us know. I am not too good with the image functions, so yea. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798837 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 That's what I don't get it is only 2.76MB Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798852 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Well if it re-renders the image 3 or 4 times, that bumps up the memory to around 12 MB. Try changing the Memory limit to 32M if it is at 8M or 16M and see if that solves the issue. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798854 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Ok I have added ini_set('memory_limit', '40M'); To my script and I'm still getting the same message , would it really chew up 40MB of memory to size a 2.7MB image? does the message Out of memory (allocated 33030144) (tried to allocate 8640 bytes) mean that I have 33030144 (33MB) and it's trying to use 8640 of it and can't? is that right Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798861 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Post the portion of the script where you call the resize image. It seems like somewhere you have a memory leak (or infinite loop). Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798907 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Ok here is what I"m doing function getImagesList($path) { $ctr = 0; if ( $img_dir = @opendir($path) ) { while ( false !== ($img_file = readdir($img_dir)) ) { // can add checks for other image file types here if ( preg_match("/(\.gif|\.jpg)$/", $img_file) ) { $images[$ctr] = $img_file; $ctr++; } } closedir($img_dir); return $images; } return false; } $image = getImagesList($path_to_images); print_r($image); echo $path_to_images.$image[0]; //resizeImage($originalImage,$toWidth,$toHeight) $images = resizeImage($path_to_images.$image[0],'300','500'); I'm just trying to resize the first image in the directory for now, thanks for all this help Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798915 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 I would use glob instead of the way you are doing it: function getImagesList($path) { $images = glob($path . "/{*.gif,*.jpg}"); // given that $path does not have the trailing slash, if it does remove that part. return (count($images) > 0)?$images:false; } The images array will return all the images in the folder, with the path in the name. See if that helps at all, Not sure if it will, but either way that should be a bit nicer/friendly than your old way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798922 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 OK added that in but getting errors This is the code <?php include 'resize.php'; ini_set('memory_limit', '40M'); $path_to_images = $_SERVER['DOCUMENT_ROOT']."/photos/".$_GET['type']."/"; // path to your images function getImagesList($path) { $images = glob($path . "{*.gif,*.jpg}"); // given that $path does not have the trailing slash, if it does remove that part. return (count($images) > 0)?$images:false; } $image = getImagesList($path_to_images); print_r($image); echo $path_to_images.$image[0]; //resizeImage($originalImage,$toWidth,$toHeight) $images = resizeImage($path_to_images.$image[0],'300','500'); ?> This is the error I'm getting [quote]Warning: Division by zero in /homepages/41/d175892888/htdocs/admin/resize.php on line 15[/quote] and here is line 15 from the function [code] $new_width = round($width * (1/$xscale)); [/code] I tried to print_r($image) but it showed nothing, any idea what I have done wrong? Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798933 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 $xscale is not being populated right. Since you cannot divide by zero, it is an error. Where is $xscale suppose to come from? I do not see that anywhere in the code... EDIT: And the function I gave you is bad, try this instead: function getImagesList($path) { $images = glob($path . "{*.gif,*.jpg}", GLOB_BRACE); // given that $path does not have the trailing slash, if it does remove that part. return (count($images) > 0)?$images:false; } I always forget about the GLOB_BRACE part. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798935 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Ok maybe my bad, I am getting confused... I was working with two scripts Here is the function I'm working with now <?php function resizeImage($originalImage,$toWidth,$toHeight){ // Get the original geometry and calculate scales list($width, $height) = getimagesize($originalImage); $xscale=$width/$toWidth; $yscale=$height/$toHeight; // Recalculate new size with default ratio if ($yscale>$xscale){ $new_width = round($width * (1/$yscale)); $new_height = round($height * (1/$yscale)); } else { $new_width = round($width * (1/$xscale)); $new_height = round($height * (1/$xscale)); } // Resize the original image $imageResized = imagecreatetruecolor($new_width, $new_height); $imageTmp = imagecreatefromjpeg ($originalImage); imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height); return $imageResized; } ?> This is how I'm calling it now, got rid of other ones for now too, sorry for the mix up <?php //include_once('easyphpthumbnail.class.php'); include 'resize.php'; ini_set('memory_limit', '40M'); $path_to_images = $_SERVER['DOCUMENT_ROOT']."/photos/".$_GET['type']."/"; // path to your images function getImagesList($path) { $images = glob($path . "{*.gif,*.jpg}"); // given that $path does not have the trailing slash, if it does remove that part. return (count($images) > 0)?$images:false; } $image = getImagesList($path_to_images); print_r($image); echo $path_to_images.$image[0]; //resizeImage($originalImage,$toWidth,$toHeight) $images = resizeImage($path_to_images.$image[0],'300','500'); ?> At some point I'm hoping to loop through all the images in the directory and resize them. I hope this has made things a little better. Again thanks for the help, you have been great. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798942 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 You need to modify this as suggested above: $images = glob($path . "{*.gif,*.jpg}", GLOB_BRACE); Add the GLOB_BRACE to it and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798943 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Ok updated your function and it print_r() is working now but still getting that Division by zero in error http://www.littlecreationsphotography.com/admin/imageResize.php?type=maternity Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798944 Share on other sites More sharing options...
Stephen68 Posted April 1, 2009 Author Share Posted April 1, 2009 Got it working with no errors but the image doesn't get resized ::sigh:: Quote Link to comment https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798969 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.