note 26195 added to function.imagecopyresized

From: Date: Tue, 22 Oct 2002 04:40:20 +0000
Subject: note 26195 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-38399@lists.php.net to get a copy of this message
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; } -- http://www.php.net/manual/en/function.imagecopyresized.php http://master.php.net/manage/user-notes.php?action=edit+26195 http://master.php.net/manage/user-notes.php?action=delete+26195 http://master.php.net/manage/user-notes.php?action=reject+26195

« previous php.notes (#38399) next »