note 103643 deleted from function.getimagesize by mazzanet
| From: | mazzanet@php.net | Date: | Tue, 26 Apr 2011 09:35:44 +0000 |
| Subject: | note 103643 deleted from function.getimagesize by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-179141@lists.php.net to get a copy of this message | ||
Note Submitter: Kristian
----
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. :(