note 69815 added to function.imagecopyresampled
| From: | zdenek dot schee at gmail dot com | Date: | Fri, 22 Sep 2006 01:14:18 +0000 |
| Subject: | note 69815 added to function.imagecopyresampled | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117400@lists.php.net to get a copy of this message | ||
This is simple function for resizing jpg files... (it used to resize even gif files to gif files
(between /* and */) but then i realized that resizing gif files causes much worse the quality than
converting to jpg and than resizing - the gif resizing is not modified for general use in the
function). The $width is a width of a new file and the $height a height of a new file... if one of
these dimensions is the same or lower than in source image the file will not be resized to bigger
image but just copied. The width/height ratio is kept in the destination file.
<?php
function resizeit($uploadsource,$destination,$width,$height)
{
if($uploadsource)
{
if(file_exists($destination))
unlink($destination);
$extension = jpg;
// The file
$filename = $uploadsource;
// Set a maximum height and width manually
//$width = 60;
//$height = 60;
// Content type
//header('Content-type: image/jpeg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
// selecting operator OR instead of AND would causes that just one lower dimension (not both of
them) would be enough to be just copied - might be useful
if(($width_orig <= $width) and ($height_orig <= $height))
{
copy($filename, $destination);
}
else
{
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
/* for case of resizing gif files
if($extension == "gif")
{
//Causes that the background of gif image with no background will be white, not black
$bgc = imagecolorallocate ($image_p, 255, 255, 255);
$tc = imagecolorallocate ($image_p, 0, 0, 0);
imagefilledrectangle ($image_p, 0, 0, $width, $height , $bgc);
$image = imagecreatefromgif($filename);
}
elseif(($extension == "jpg") or ($extension == "jpeg"))*/
$image = imagecreatefromjpeg($uploadsource);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
/* for case of resizing gif files
if($extension == "gif")
imagegif($image_p, "znacky/".$id."_60.gif", 100);
elseif(($extension == "jpg") or ($extension == "jpeg"))
*/
//you can change the quality from 100 to another integer .. 80 is probably the most common
imagejpeg($image_p, $destination, 100);
chmod($destination,0777);
}
}
}
?>
----
Server IP: 147.32.160.17
Probable Submitter: 84.42.174.55
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresampled.php
Edit -- https://master.php.net/note/edit/69815
Del: integrated -- https://master.php.net/note/delete/69815/integrated
Del: useless -- https://master.php.net/note/delete/69815/useless
Del: bad code -- https://master.php.net/note/delete/69815/bad+code
Del: spam -- https://master.php.net/note/delete/69815/spam
Del: non-english -- https://master.php.net/note/delete/69815/non-english
Del: in docs -- https://master.php.net/note/delete/69815/in+docs
Del: other reasons-- https://master.php.net/note/delete/69815
Reject -- https://master.php.net/note/reject/69815
Search -- https://master.php.net/manage/user-notes.php