使用PHP的GD2裁剪 + 缩放图片

发布时间:2017-1-21 12:22:03 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"使用PHP的GD2裁剪 + 缩放图片 ",主要涉及到使用PHP的GD2裁剪 + 缩放图片 方面的内容,对于使用PHP的GD2裁剪 + 缩放图片 感兴趣的同学可以参考一下。

	/**	 * 裁剪 + 缩放图片	 * @param array $params 包含x,y,width,height,path	 * @return string	 */	public function tailer_image($params){		//$target_width,$target_height是缩放图片的大小		$target_width = 256;		$target_height = 256;		$source_x = intval($params['x']);		$source_y = intval($params['y']);		$cropped_width = intval($params['width']);		$cropped_height = intval($params['height']);		$source_path = trim($params['path']);		$path = 'files/uploads/images/';		list($source_width,$source_height) = getimagesize($source_path);		//获取图片的扩展名		$extend_name = ltrim(strrchr(strtolower($source_path),'.'),'.');		if(!in_array($extend_name,array('jpg','jpeg','png'))){			return array(						array('message'=>"图片扩展名不正确,只允许'jpg','jpeg','png'"),						400					);		}		switch ($extend_name) {			case 'jpg':				$source_image = imagecreatefromjpeg($source_path);				break;			case 'jpeg':				$source_image = imagecreatefromjpeg($source_path);				break;			case 'png':				$source_image = imagecreatefrompng($source_path);				break;		}				$target_image  = imagecreatetruecolor($target_width, $target_height);		$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);		// 裁剪		imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);		// 缩放		imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);		$filename = md5(microtime()).'.'.$extend_name;		$dst_url = $path.$filename;		//输出图像到$dst_url文件		imagejpeg($target_image, $dst_url);		return $dst_url;	}

上一篇:sqlalchemy学习
下一篇:Creating Custom Login Screen In Oracle Forms 10g

相关文章

相关评论