/ Published in: PHP
This properly handles transparencies for PNG/GIF.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * 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; } // imagecolorallocatealpha($new_img, 0, 0, 0, 0); // get and reallocate transparency-color if ($transindex >= 0) { $transindex = imagecolorallocatealpha($new_img, $transcol['red'], $transcol['green'], $transcol['blue'], 127); } else if ($transindex == -1) { } // stuff the existing file off in to a backup directory // we need to get the end path // make sure that the directory exists } } // write the image out to the new file, using imagepng/imagegif/etc return $res; } else { return false; } }