| Versionen | |
|---|---|
| drupal6 | image_scale( |
| drupal7 | image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) |
Scales an image to the given width and height while maintaining aspect ratio.
The resulting image can be smaller for one or both target dimensions.
see image_load()
$image An image object returned by image_load().
$width The target width, in pixels. This value is omitted then the scaling will based only on the height value.
$height The target height, in pixels. This value is omitted then the scaling will based only on the width value.
$upscale Boolean indicating that files smaller than the dimensions will be scalled up. This generally results in a low quality image.
TRUE or FALSE, based on success.
includes/
<?php
function image_scale(stdClass $image, $width = NULL, $height = NULL, $upscale = FALSE) {
$aspect = $image->info['height'] / $image->info['width'];
if ($upscale) {
// Set width/height according to aspect ratio if either is empty.
$width = !empty($width) ? $width : $height / $aspect;
$height = !empty($height) ? $height : $width / $aspect;
}
else {
// Set impossibly large values if the width and height aren't set.
$width = !empty($width) ? $width : 9999999;
$height = !empty($height) ? $height : 9999999;
// Don't scale up.
if (round($width) >= $image->info['width'] && round($height) >= $image->info['height']) {
return TRUE;
}
}
if ($aspect < $height / $width) {
$height = $width * $aspect;
}
else {
$width = $height / $aspect;
}
return image_resize($image, $width, $height);
}
?>
Kommentare
Kommentar hinzufügen