Return to Snippet

Revision: 21150
at December 4, 2009 14:16 by gms8994


Initial Code
/**
 * 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($source)) { error_log("$source doesn't exist"); return false; }
	if (file_exists($dest) && !$overwrite) { error_log("$dest exists, and we're not overwriting"); return false; }
	list($width, $height, $type, $attr) = getimagesize($source);
	if ($width > $height) {
		$n_height = ($n_width / $width) * $height;
	} else {
		$n_width = ($n_height / $height) * $width;
	}

	$mime_type = image_type_to_mime_type($type);
	$mime_type = preg_replace("/^.*?\//", "", $mime_type);

	// call the appropriate imagecreatefrom function for this image
	if (function_exists("imagecreatefrom" . $mime_type)) {
		$img = call_user_func("imagecreatefrom" . $mime_type, $source);
	} else {
		display_error("invalid_image_format");
		error_log("imagecreatefrom{$mime_type} doesn't exist");
		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
	if (function_exists("image" . $mime_type)) {
		// we need to get the end path
		$my_local_dest = preg_replace("/^(.*)\/(.*?)$/", "$1/source/$2", $dest);
		$my_local_dest = preg_replace("/^(.*)_.*/", "$1.$mime_type", $my_local_dest);
		// make sure that the directory exists
        if (! is_dir(dirname($my_local_dest))) {
            $res = mkdir(dirname($my_local_dest));
        }
		$res = call_user_func("image" . $mime_type, $img, $my_local_dest);
		if (!$res) { error_log("image{$mime_type} failed!!!!"); }
	}

	imagedestroy($img);

	// write the image out to the new file, using imagepng/imagegif/etc
	$my_dest_type = preg_replace("/^.*\.(.*?)$/", "$1", $dest);
	if (function_exists("image" . $my_dest_type)) {
		$res = call_user_func("image" . $my_dest_type, $new_img, $dest);
		imagedestroy($new_img);
		return $res;
	} else {
		error_log("image{$my_dest_type} doesn't exist");
		imagedestroy($new_img);
		return false;
	}

}

Initial URL

                                

Initial Description
This properly handles transparencies for PNG/GIF.

Initial Title
Resize and Name Image

Initial Tags
resize, image

Initial Language
PHP