PHP 调整大小和命名图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 调整大小和命名图像相关的知识,希望对你有一定的参考价值。

/**
 * 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 <glens@networldalliance.com>
 */
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;
	}

}

以上是关于PHP 调整大小和命名图像的主要内容,如果未能解决你的问题,请参考以下文章

PHP:图像调整大小和裁剪为纵向

调整图像大小并命名

PHP图像动态调整大小和圆角图像

PHP图像调整大小和裁剪功能

PHP 如何使用PHP调整图像大小 - 图像大小调整脚本

PHP调整图像大小固定大小但保持方向