/ Published in: PHP
This properly handles transparencies for PNG/GIF.
Expand |
Embed | Plain Text
/** * Resizes and renames an image file * @return resource An image identifier * @return false on error * @param string $source Path to source image * @param string $dest Path to output image * @param int $n_height New height to set image to * @param int $n_width New width to set image to * @param bool $overwrite Allow overwrite of image file * @author Glen Solsberry <[email protected]> */ function resize_and_name_image($source, $dest, $n_height, $n_width, $overwrite = false) { if (file_exists($dest) && !$overwrite) { error_log("$dest exists, and we're not overwriting"); return false; } if ($width > $height) { $n_height = ($n_width / $width) * $height; } else { $n_width = ($n_height / $height) * $width; } // call the appropriate imagecreatefrom function for this image } else { display_error("invalid_image_format"); return false; } $new_img = imagecreatetruecolor($n_width, $n_height); // imagecolorallocatealpha($new_img, 0, 0, 0, 0); // get and reallocate transparency-color $transindex = imagecolortransparent($img); if ($transindex >= 0) { $transcol = imagecolorsforindex($img, $transindex); $transindex = imagecolorallocatealpha($new_img, $transcol['red'], $transcol['green'], $transcol['blue'], 127); imagefill($new_img, 0, 0, $transindex); } else if ($transindex == -1) { imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0)); imagealphablending($new_img, false); imagesavealpha($new_img, true); } imagecopyresampled($new_img, $img, 0, 0, 0, 0, $n_width, $n_height, $width, $height); // stuff the existing file off in to a backup directory // we need to get the end path // make sure that the directory exists } } imagedestroy($img); // write the image out to the new file, using imagepng/imagegif/etc imagedestroy($new_img); return $res; } else { imagedestroy($new_img); return false; } }
You need to login to post a comment.
