note 69191 added to function.imagecopyresampled
| From: | dannyt_ro at yahoo dot com | Date: | Sat, 26 Aug 2006 08:11:38 +0000 |
| Subject: | note 69191 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116337@lists.php.net to get a copy of this message | ||
I modified a script in order to make a function that will resize an image to the desired dimensions
keeping the proportions
This function will delete the original "jpg" replacing it with the modified one. Good
Luck!
<?
function resizeImage ($sourcefile, $dest_x, $dest_y, jpegqual)
{
/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
$source_id = imageCreateFromJPEG("$sourcefile");
//Keep the proportions on the resizd image
if ($picsize[0]>$picsize[1]){
$ratio=$picsize[0]/$dest_x;
$dest_y=$picsize[1]/$ratio;
}
else{
$ratio=$picsize[1]/$dest_y;
$dest_x=$picsize[0]/$ratio;
}
/* 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 */
//Delete of the original file so that the new file cand be uploaded
unlink($sourcefile);
//Writing the desired image instead of the original image
imagejpeg ($target_id,"$sourcefile",$jpegqual);
return true;
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 86.35.34.136
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/69191
Del: integrated -- https://master.php.net/note/delete/69191/integrated
Del: useless -- https://master.php.net/note/delete/69191/useless
Del: bad code -- https://master.php.net/note/delete/69191/bad+code
Del: spam -- https://master.php.net/note/delete/69191/spam
Del: non-english -- https://master.php.net/note/delete/69191/non-english
Del: in docs -- https://master.php.net/note/delete/69191/in+docs
Del: other reasons-- https://master.php.net/note/delete/69191
Reject -- https://master.php.net/note/reject/69191
Search -- https://master.php.net/manage/user-notes.php