note 26195 deleted from function.imagecopyresized by cece
| From: | cece@php.net | Date: | Sat, 16 Aug 2003 20:07:07 +0000 |
| Subject: | note 26195 deleted from function.imagecopyresized by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-54448@lists.php.net to get a copy of this message | ||
Note Submitter: spamhole@gmx.at
----
the following function can be used to scale an image down without changing the ratio between x and
y. this makes resized images/thumbnails look more neutural.
for example a picture of 1024x768 has a ratio of 4:3 (1024/4 * 3 = 768).. resizing it to 100x50
would result in 2:1. with this function it will automatically get resized to 66x50 instead, which is
much closer to 4:3.
function loadjpeg($path, $max_x, $max_y) {
$im = @imagecreatefromjpeg($path);
if (!$im) {
$im = imagecreate(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
imagestring($im, 1, 5, 5, "Error loading $path", $tc);
}
if ($max_x != 0 && $max_y != 0) {
$x = imagesx($im);
$y = imagesy($im);
if ($x > $max_x) {
$y = (int)floor($y * ($max_x / $x));
$x = $max_x;
}
if ($y > $max_y) {
$x = (int)floor($x * ($max_y / $y));
$y = $max_y;
}
if (imagesx($im) != $x || imagesy($im) != $y) {
$tmp = imagecreate($x, $y);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $x, $y, imagesx($im), imagesy($im));
imagedestroy($im);
$im = $tmp;
}
}
return $im;
}