Jump to content

Recommended Posts

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..

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/
Share on other sites

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

	}

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798827
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798837
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798861
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798915
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798922
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798933
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798935
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/152086-resizing-help/#findComment-798942
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.