PHP JPEG resampling - php

i have this script to take original picture , resample it twice , for thumbnail and preview.This script works fine , even though You may find some weaknesses of syntactic fashion , im sure.The script as it is , is not subject of my question. I am wondering whether i am supposed to somehow clear memory afterwards.Am i flooding my server with data ? or is this fine and clears itself afterwards.Iam asking because this script will handle my gallery , and it is expected to handle multiple files at once.
script is written like this :
$filename = $DumpHere.$Processed;
// Get new dimensions
list($width, $height) = getimagesize($filename);
// Resample thumbnail
$image_p = imagecreatetruecolor(70, 70);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 70, 70, $width, $height);
// Output Thumbnail
imagejpeg($image_p, $ThumbsFolder.'thumb_'.$Processed, 100);
// Resample preview
$image_p = imagecreatetruecolor(500, 300);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, 500, 300, $width, $height);
// Output Preview
imagejpeg($image_p, $PreviewFolder.'preview_'.$Processed, 100);
just to be clear
$DumpHere
is path to a folder containing original files before processing.Thanks for any help.

You want to use imagedestroy() on your resources, so just add:
imagedestroy($image_p);
imagedestroy($image);
At the end and that will free up the memory. PHP is pretty good about getting rid of memory on it's own. For example, once your script ends, all the memory is freed up. But this is the method to explicitly return those resources to the system.

Related

PHP compressing image into thumbnail reduces thumbnail quality alot

I'm creating thumbnails in PHP, but when I display the thumbnail on screen, the quality drastically drops. I'll post the php resample code:
$image_p = imagecreatetruecolor($newwidth, $newheight);
imagejpeg($image_p, null, 99);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $filewidth, $fileheight);
$imageformat($image_p, $_SERVER["DOCUMENT_ROOT"]."$filepath_thumb"); // Thumbnail Folder
How do I fix it. I've had people suggest tweaking the imagejpeg(); line. Any suggestions?

can't resize an image using php

I have a php code to get an image and resize it. Can't get the output as expected.. Please help me figure out what the exact problem is..!!
<?php
$picture_source = 'image.png';
if ($picture_source != null){
$img1=file_get_contents($picture_source);
$new_img1 = resizeImage($img1, 200, 200);
file_put_contents("i1.png", $new_img1);
}
function resizeImage($img,$newwidth,$newheight) {
list($width, $height) = getimagesizefromstring($img);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $thumb;
}
You've got some confusion with your variable names and types. You're trying to deal with a filename, the file contents, and an image resource at the same time, and you're using the wrong ones for a few of the functions.
imagecopyresampled takes two image resources (source and destination), but you're passing it the file contents in place of the source.
file_put_contents takes a string for the file contents, but you're passing it the resized image resource.
PHP has native functions for reading the image dimensions and creating an image resource from a filename, so you shouldn't need to ever have the source file contents available as a string.
If you change a few of the variable names and function calls around, you end up with:
<?php
$sourceFilename = 'image.png';
if ($sourceFilename != null){
$newImg = resizeImage($sourceFilename, 200, 200);
imagepng($newImg, "i1.png");
}
function resizeImage($imgFilename, $newWidth, $newHeight) {
list($width, $height) = getimagesize($imgFilename);
$img = imagecreatefrompng($imgFilename);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $thumb;
}
Also, you should turn on PHP notices and warnings in your development environment - they will have been trying to help you.

PHP - Resized image has larger file size

Does anyone have a work around for this? I think that the image is already optimised. It then get's resized and it sheds its optimization and despite the picture being made narrowly smaller. The file size increases.
Has anyone come across this before.
I have an image that was saved at 50% quality. If I copy -> resize -> save at 70% it gains 80Kb..
Is there a solution which allows me to detect the quality of the image before it goes in?
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagesavealpha($new_image, true);
$trans_colour = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
imagefill($new_image, 0, 0, $trans_colour);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
...
imagejpeg($this->image,$filename,$compression);
Technically, the quality rate of the conversion is not derivable from the jpeg data, it just tells the converter which tradeoff to make between size and quality.
Some converters store it in the EXIF data of the JPEG header though, so if that is still present you can use it with exif_read_data on it, and see if the compression information is returned.

imagecopyresampled makes black box but not resampled image

I am working to resize and resample some jpeg images using PHP. It take any image greater than 500px by 500px and make the largest side 500px. This should be relatively simple but every time I run the script it makes a black jpeg. The jpeg created has the proper dimensions but does not include the resized image. The GD library is enabled, and I have made sure it is finding the original image. I've been looking at this block of code for a day and half with no luck, what am I not seeing?
<?php
$testimage = 'SandyCayCaribbeanbeach.jpg';
$testfolder = "testimage/testimage.jpg";
list($orgwidth, $orgheight, $type, $attr) = getimagesize($testimage);
echo "org. width " . $orgwidth . "px" . "<br />";
echo "org. height " . $orgheight . "px" . "<br />";
if($orgwidth > 500 || $orgheight > 500){
if($orgwidth > $orgheight){
header('Content-type: image/jpeg');
$ratio = $orgwidth/500;
$newwidth = floor($orgwidth/$ratio);
$newheight = floor($orgheight/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
else{
header('Content-type: image/jpeg');
$ratio = $orgheight/500;
$newheight = floor($orgheight/$ratio);
$newwidth = floor($orgwidth/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
}
?>
Firstly make sure you have error reporting turned on. Also make sure it can find the source image "SandyCayBaribbeanbeach.jpg".
A simple if(file_exists()) check before handling the image resizing will help trap errors.
I found that I had to specify a full path to the image, not a URL i.e.
/path/to/image.jpg
instead of
http://www.blah.com/image.jpg
to get this to work properly. Hope it helps someone.
Double-check to make sure your source image is truly a JPEG. If you are running Windows, open it up in MS Paint and re-save as a JPEG. This will help rule out the possibility of it being a different format.
I also fought this for a while in a piece of my code recently, and found that imagecopyresampled will even return 1 if the dimensions are not defined. Make sure that your source height and width are set.

Save an image Resized with PHP

I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038

Categories