On this page
function image_gd_create_tmp
image_gd_create_tmp(stdClass $image, $width, $height)
Create a truecolor image preserving transparency from a provided image.
Parameters
$image: An image object.
$width: The new width of the new image, in pixels.
$height: The new height of the new image, in pixels.
Return value
A GD image handle.
Related topics
File
- modules/system/image.gd.inc, line 337
- GD2 toolkit for image manipulation within Drupal.
Code
function image_gd_create_tmp(stdClass $image, $width, $height) {
$res = imagecreatetruecolor($width, $height);
if ($image->info['extension'] == 'gif') {
// Find out if a transparent color is set, will return -1 if no
// transparent color has been defined in the image.
$transparent = imagecolortransparent($image->resource);
if ($transparent >= 0) {
// Find out the number of colors in the image palette. It will be 0 for
// truecolor images.
$palette_size = imagecolorstotal($image->resource);
if ($palette_size == 0 || $transparent < $palette_size) {
// Set the transparent color in the new resource, either if it is a
// truecolor image or if the transparent color is part of the palette.
// Since the index of the transparency color is a property of the
// image rather than of the palette, it is possible that an image
// could be created with this index set outside the palette size (see
// http://stackoverflow.com/a/3898007).
$transparent_color = imagecolorsforindex($image->resource, $transparent);
$transparent = imagecolorallocate($res, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
// Flood with our new transparent color.
imagefill($res, 0, 0, $transparent);
imagecolortransparent($res, $transparent);
}
else {
imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
}
}
}
elseif ($image->info['extension'] == 'png') {
imagealphablending($res, FALSE);
$transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
imagefill($res, 0, 0, $transparency);
imagealphablending($res, TRUE);
imagesavealpha($res, TRUE);
}
else {
imagefill($res, 0, 0, imagecolorallocate($res, 255, 255, 255));
}
return $res;
}
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/modules!system!image.gd.inc/function/image_gd_create_tmp/7.x