Forum Moderators: coopster

Message Too Old, No Replies

have PHP resize jpg pictures

is it possible?

         

globay

11:55 pm on Jan 28, 2003 (gmt 0)

10+ Year Member



I have never used PHP to transform pictures before, so I am quite new to that stuff.

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

Birdman

12:42 am on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's funny, I am on the same wavelength. I'd like to create a thumbnail from the original upload.

I'm all ears too;)

CoryZ

12:46 am on Jan 29, 2003 (gmt 0)

10+ Year Member



Hi,

There is a PHP interface to ImageMagick which will easily resize graphics. Do a search for imagick and PHP.

scratch

9:11 am on Jan 29, 2003 (gmt 0)

10+ Year Member



Or use gd, there are snippets in the manual on php.net if you look at the comments that are pretty elaborate. (of course gd only does jpg, png, bmp, and only loads gif for the most part)

[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;
}

globay

12:19 pm on Jan 29, 2003 (gmt 0)

10+ Year Member



Thank you for your answers! Now I will be able to do what I need. Also there are several websites on the net that offer free PHP scripts. There I found some scripts to resize pictures for downloading that I just had to understand.

globay

globay

6:52 pm on Jan 29, 2003 (gmt 0)

10+ Year Member



Somehow this does not work for me. I tried the Code that scratch posted, but when I try to call up the page, it tells me "page could not be found". Strangly this happend with Code, I got from other web resources, too.

Could be that my hosting company does not provide something that is necessary?

Thanks!

Birdman

8:25 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

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.

jatar_k

8:31 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you probably don't have permission to write to that dir, need to change permissions.

I do it through ftp usually. I use cute and when you right click on a dir there is an option for CHMOD. Need to see who can write to it. I usually make it writable to all.

Birdman

8:49 pm on Jan 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very weird? I already had permissions set to 777, then I tinkered with the filepath to no avail. So I put it back how it was and it worked!

Whew, that was hours of frustration. That pic better look good :)

Thanks again jatar_k

globay

10:48 pm on Jan 29, 2003 (gmt 0)

10+ Year Member



So finally I got it to work. Somehow the quality of the image is very bad, though I use 100% JPEG quality.

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