note 18728 deleted from ref.image by tularis

From: Date: Sun, 29 Jun 2008 21:13:21 +0000
Subject: note 18728 deleted from ref.image by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-140984@lists.php.net to get a copy of this message
Note Submitter: stefan dot wehowsky at profilschmiede dot de ---- 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; }

« previous php.notes (#140984) next »