Thumbnail Generator

Use this function can generator a new image with any size , though it's sounds like this , don't use this function to generator the image that width and height big than original image , it will anamorphose badly .

<?php
/**
 * the main use of this function is to generator thumbnail
 * make sure your GD library are enabled
 *
 * @param string $image_path           the image path
 * @param string $thumb_image_path     the generated thumbnail path
 * @param number $resize_width         thumbnail width
 * @param number $resize_heigth        thumbnail height
 * @return success return true , failue on false
 */
function image_resize($image_path,$thumb_image_path,$resize_width,$resize_heigth){
	$image_array = array();
	preg_match('/.+\.(\w+)/',$image_path,$match);
	switch ($match[1]){
		case 'gd2':
			$image = imagecreatefromgd2($image_path);
			break;
		case 'gd':
			$image = imagecreatefromgd($image_path);
			break;
		case 'gif':
			$image = imagecreatefromgif($image_path);
			break;
		case 'jpg':
			$image = imagecreatefromjpeg($image_path);
			break;
		case 'png':
			$image = imagecreatefrompng($image_path);
			break;
		case 'wbmp':
			$image = imagecreatefromwbmp($image_path);
			break;
		case 'xbm':
			$image = imagecreatefromxbm($image_path);
			break;
		case 'xpm':
			$image = imagecreatefromxpm($image_path);
			break;
		default:
			return false;
	}
	list($width,$height) = getimagesize($image_path);
	$newimage = imagecreatetruecolor($resize_width, $resize_heigth);
	imagecopyresampled($newimage, $image, 0, 0, 0, 0, $resize_width, $resize_heigth, $width, $height);
	if (imagejpeg($newimage,$thumb_image_path)) {
		imagedestroy($image);
		imagedestroy($newimage);
		return true;
	}else {
		imagedestroy($image);
		imagedestroy($newimage);
		return false;
	}
}

?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值