resize multiple images in php without exceeding memory limit - php

I am currently trying to get a form which will allow multiple images to be uploaded and resized on the server using PHP. Each uploaded image by the client is around 2.5mb in size.
I am currently using the move_uploaded_file() function.
There are no issues to move the files onto the server. The problem arises when I try to crop. Having not ImageMagick on my host I am using this setup (not all the code just what's relevant, this is in a loop with $width etc. changing for the different crop sizes)
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
As it stands this will only work for 2 images. If there is 3 or more submitted I get a 'memory exhausted' error. I have researched into this as my memory limit is 120mb. Apparently the imagecreatefromjpeg function uses a lot of memory, especially if the file has a large resolution (which mine does - hence why I need them cropped/resized).
Does anyone know of a more efficient approach to this task? I have researched on google but everyone uses the same technique I am.

Use imagedestroy to clear any memory associated with $image and $image_p :
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, $output_filename, 80);
imagedestroy($image);
imagedestroy($image_p);

Related

What is GD library, how to include it and crop an image in php?

Hey guys i am trying to crop uploaded image using php using following code.
if(isset($_POST["Crop"])){
$source_x=$_POST['x_value'];
$source_y=$_POST['y_value'];
$width=$_POST['w_value'];
$height=$_POST['h_value'];
$src="assets/images/uploads/1509432972_Koala.jpg";
$crop = imagecreatetruecolor($width,$height);
imagecopy ($crop, $src, 0, 0, $source_x, $source_y, $width, $height);
imagejpeg($crop);
imagedestroy($src);
imagedestroy($crop);
}
The problem that it is not working properly. Since there are lot of posts there to solve this, found a solution called GD library how to include it and crop all kind of images.

How can I upload a generated image via FTP without saving it locally

I have successfully overlaid a .png file atop a .jpg file. The problem is, I need to FTP this file to another server, preferably without ever saving it locally. I've looked into temp files with PHP, but can't make sense of it.
Specifically, I'm tossing some variables to a function. The function finds the right path for the PNG file from the database, lays it over the JPEG file from the path given. The combined image needs uploaded to an off-site server... this is where my coding experience fails me. If I'm writing this new image to a temp file, how to I pass its path back to be uploaded, rather than the image itself.
$overlayFilePath = '../overlays/'.$row["Overlay1"];
$png = imagecreatefrompng($overlayFilePath);
$jpeg = imagecreatefromjpeg($local_file);
list($width, $height) = getimagesize($local_file);
list($newwidth, $newheight) = getimagesize($overlayFilePath);
$out = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresampled($out, $png, 0, 0, 0, 0, $newwidth, $newheight, $newwidth, $newheight);
$combinedImage = imagejpeg($out, NULL, 100);
imagedestroy($out);
return $combinedImage; // this is the issue. I need to return an address to the image, but I do not want to have thousands of images being saved locally on this server.
$conn->close();
}
?>

Multiple image resize using php at once

I am using PHP GD2 to resize and crop image using below function
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
But I want to create many samples of a single image of different sizes. I am currently repeating same function in single file to get different sizes. But is there any way, perhaps using array or something using which I can achieve the same result?

How can I transform a true color image to a jpeg image that I can use in a variable in php

I am trying to create the jpeg image using the imagejpeg but that outputs the image to browser and I don't want that.
$im = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($im, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$im=imagejpeg($im, 'test.jpg', 100);
It comes up as if it doesn't have a jpg stored inside $im
I am trying to re-size an image and then work with it afterwards in the jpg format
In PHP, an image hasn't any type once it was created (from imageCreateFromJpeg(), imageCreateTrueColor(), …) : this is simply a ressource.
The conversion in a specific type takes place afterward, when you use imageJpeg() for instance. imageJpeg() outputs the image to browser or saves it in a file. That's all, it returns nothing more than a boolean which say if the conversion was a success or not.

Making alpha PNGs with PHP GD

I've got a problem making alpha PNGs with PHP GD. I don't have imageMagik etc.
Though the images load perfectly well in-browser and in GFX programs, I'm getting problems with Flash AS3 (actionscript) understanding the files. It complains of being an unknown type. But, exporting these files from Fireworks to the same spec works fine. So I'm suggesting it's something wrong with the formatting in PHP GD.
There seems to be a number of ways of doing this, with several similar functions; so maybe this isn't right?
$image_p = imagecreatetruecolor($width_orig, $height_orig);
$image = imagecreatefrompng($filename);
imagealphablending($image_p, false);
ImageSaveAlpha($image_p, true);
ImageFill($image_p, 0, 0, IMG_COLOR_TRANSPARENT);
imagealphablending($image_p, true);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width_orig, $height_orig, $width_orig, $height_orig);
imagepng($image_p, "new2/".$filename, 0);
imagedestroy($image_p);
This just takes files it's given and puts them into new files with a specified width/height - for this example it's same as original but in production it resizes, which is why I'm resampling.
To keep the transparency you should do
imagealphablending($image_p, false);
instead of "true". Maybe that will solve the format problem too.

Categories