note 103643 added to function.getimagesize
| From: | Kristian at osu1 dot php dot net | Date: | Tue, 26 Apr 2011 02:57:58 +0000 |
| Subject: | note 103643 added to function.getimagesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179130@lists.php.net to get a copy of this message | ||
I wrote a similar script for resizing images if they are above certain dimensions, used in my case
for rescaling adverts that are too big for the assigned advert region.
(Note that this was part of a larger function which I've modified for this post.)
function resize_img_if_required($image, $max_width = 0, $max_height = 0, $alt_text = "")
{
if ($max_width < 0) { $max_width = 0; }
if ($max_height < 0) { $max_height = 0; }
$image_size = getimagesize($image);
if ($max_width > 0 && $max_height == 0) {
$image_x_ratio = ($image_size[0] / $max_width);
if ($image_x_ratio > 1)
{
$output = '<img width="' . $max_width . '"
height="'.floor($image_size[1] / $image_x_ratio) . '" src="' . $image
. '"';
}
} elseif ($max_width == 0 && $max_height > 0) {
$image_y_ratio = ($image_size[1] / $max_height);
if ($image_y_ratio > 1)
{
$output = '<img width="' . $max_width . '"
height="'.floor($image_size[1] / $image_x_ratio) . '" src="' . $image
. '"';
}
} else {
$image_x_ratio = ($image_size[0] / $max_width);
$image_y_ratio = ($image_size[1] / $max_height);
if ($image_x_ratio > 1 || $image_y_ratio > 1)
{
if ($image_x_ratio > $image_y_ratio)
{
$output = '<img width="' . $max_width . '"
height="'.floor($image_size[1] / $image_x_ratio) . '" src="' . $image
. '"';
} else {
$output = '<img width="' . floor($image_size[0] / $image_y_ratio) .
'" height="'. $max_width . '" src="' . $image .
'"';
}
}
}
if (empty($output)) // no output so far means the image did not need resizing.
{
$output = '<img width="' . $image_size[0] . '" height="' .
$image_size[1] . '"src="'.$image.'"';
}
if (!empty ($alt_text))
{
$output .= ' alt="' . $alt_text . '"';
}
$output .= ' />';
return $output;
}
PS: Sorry for not knowing how to make it colour coded. :(
----
Server IP: 69.147.83.197
Probable Submitter: 83.117.121.43
----
Manual Page -- http://www.php.net/manual/en/function.getimagesize.php
Edit -- https://master.php.net/note/edit/103643
Del: integrated -- https://master.php.net/note/delete/103643/integrated
Del: useless -- https://master.php.net/note/delete/103643/useless
Del: bad code -- https://master.php.net/note/delete/103643/bad+code
Del: spam -- https://master.php.net/note/delete/103643/spam
Del: non-english -- https://master.php.net/note/delete/103643/non-english
Del: in docs -- https://master.php.net/note/delete/103643/in+docs
Del: other reasons-- https://master.php.net/note/delete/103643
Reject -- https://master.php.net/note/reject/103643
Search -- https://master.php.net/manage/user-notes.php