Forum Moderators: coopster
I have a password protected-area, where members can upload images. These jpg-files are quite large in size. Now I want PHP to reduce size and quality of the picture. Is it possible to save the the new pictures?
thanks for your help!
globay
[php.net...]
on this page i found the following, hope it helps.....
stefan dot wehowsky at profilschmiede dot de
02-Feb-2002 04:57
If you ever want to resize a picture (maybe in order to create a thumbnail), this small function
should help you. It also gives you an idea of how some of the basic image functions of PHP can be
used.
/* resizeToFile resizes a picture and writes it to the harddisk
*
* $sourcefile = the filename of the picture that is going to be resized
* $dest_x = X-Size of the target picture in pixels
* $dest_y = Y-Size of the target picture in pixels
* $targetfile = The name under which the resized picture will be stored
* $jpegqual = The Compression-Rate that is to be used
*/
function resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual)
{
/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
$source_id = imageCreateFromJPEG("$sourcefile");
/* Create a new image object (not neccessarily true colour) */
$target_id=imagecreatetruecolor($dest_x, $dest_y);
/* Resize the original picture and copy it into the just created image
object. Because of the lack of space I had to wrap the parameters to
several lines. I recommend putting them in one line in order keep your
code clean and readable */
$target_pic=imagecopyresampled($target_id,$source_id,
0,0,0,0,
$dest_x,$dest_y,
$source_x,$source_y);
/* Create a jpeg with the quality of "$jpegqual" out of the
image object "$target_pic".
This will be saved as $targetfile */
imagejpeg ($target_id,"$targetfile",$jpegqual);
return true;
}
Could be that my hosting company does not provide something that is necessary?
Thanks!
I altered the code above because my host doesn't have GD 2.0.1 support. So I replaced imagecreatetruecolor() with imagecreate() and imagecopyresampled() with imagecopyresized().
That got rid of all the errors except the one below. It seems like it wants to overwrite an existing file. I need it to "create" the file.
Warning: imagejpeg: unable to open '/home/bridal/public_html/ path/images/thumbs/51.jpg' for writing
$new_thumb = "thumbs/$new_pic";
$sourcefile = "$uploaddir$new_pic";
$dest_x = 75;
$dest_y = 100;
$targetfile = "$uploaddir$new_thumb";
$jpegqual = 75;/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
$source_id = imageCreatefromjpeg("$sourcefile");/* Create a new image object (not neccessarily true colour) */
$target_id=imagecreate($dest_x, $dest_y);
$target_pic=imagecopyresized
($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y);imagejpeg ($target_id,"$targetfile",$jpegqual);
Any ideas would be greatly appreciated.
I read, using imagecreatetruecolor instead of imagecreate would return a better result, but somehow when I switch the code, nothing works anymore.
1. Are there any other methods to improve the quality of a picture?
2. I know that the newest GD is installed on the server. Why do I get a "Page not found" error after just changing that little tag?
thanks for your help!
globay