note 18728 added to ref.image

From: Date: Sat, 02 Feb 2002 08:57:59 +0000
Subject: note 18728 added to ref.image
Groups: php.notes 
Request: Send a blank email to php-notes+get-26152@lists.php.net to get a copy of this message
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; } -- http://www.php.net/manual/en/ref.image.php http://master.php.net/manage/user-notes.php?action=edit+18728 http://master.php.net/manage/user-notes.php?action=delete+18728 http://master.php.net/manage/user-notes.php?action=reject+18728

« previous php.notes (#26152) next »