您好,欢迎来到思海网络,我们将竭诚为您提供优质的服务! 诚征网络推广 | 网站备案 | 帮助中心 | 软件下载 | 购买流程 | 付款方式 | 联系我们 [ 会员登录/注册 ]
促销推广
客服中心
业务咨询
有事点击这里…  531199185
有事点击这里…  61352289
点击这里给我发消息  81721488
有事点击这里…  376585780
有事点击这里…  872642803
有事点击这里…  459248018
有事点击这里…  61352288
有事点击这里…  380791050
技术支持
有事点击这里…  714236853
有事点击这里…  719304487
有事点击这里…  1208894568
有事点击这里…  61352289
在线客服
有事点击这里…  531199185
有事点击这里…  61352288
有事点击这里…  983054746
有事点击这里…  893984210
当前位置:首页 >> 技术文章 >> 文章浏览
技术文章

php生成图片缩略图的函数

添加时间:2014-11-29 17:54:31  添加: 思海网络 

具体用法如下:

$a=thumbImage('1.jpg',200,100,'2.jpg');//将1.jpg生成宽为200,高为100的缩略图,并保存为2.jpg
print_r($a);

打印结果:

Array(
    [status] => 0
    [file] => 1.jpg
    [width] => 200
    [height] => 200
    [savePath] => 2.jpg
    [info] => 缩略图片生成成功
)

$a=thumbImage('1.jpg',200,100);//将1.jpg生成宽为200,高为100的缩略图,并直接覆盖1.jpg,不生成新图片

函数代码如下:

<?php
/*
*php生成图片缩略图的函数
*www.Alixixi.com
*/
function thumbImage($file,$thumbWidth,$thumbHeight,$savePath='',$isCut=true,$quality=100){
	$result=array('status'=>0,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'');
	if(!file_exists($file)){
		return array('status'=>1,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件不存在');
	}
	//检测文件类型
	$fp=fopen($file,'rb');
	$str=fread($fp,2); //只读2字节
	fclose($fp);
	$str=@unpack("c2chars",$str);
	$typeCode=intval($str['chars1'].$str['chars2']);
	$fileType='';
	switch($typeCode){
		case 255216:
			$fileType='jpg';
		break;
		case 7173:
			$fileType='gif';
		break;
		case 6677:
			$fileType='bmp';
		break;
		case 13780:
			$fileType='png';
		break;
		default:
			$fileType=$typeCode;
		break;
	}
	if($str['chars1']=='-1'&&$str['chars2']=='-40'){
		$fileType='jpg';
	}
	if($str['chars1']=='-119'&&$str['chars2']=='80'){
		$fileType='png';
	}
	if(!in_array($fileType,array('jpg','gif','bmp','png'))){
		return array('status'=>2,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片文件类型不正确:'.$fileType);
	}
	//图片缩放处理
	if($fileType=='jpg'){
		$im=imagecreatefromjpeg($file);
	}
	if($fileType=='gif'){
		$im=imagecreatefromgif($file);
	}
	if($fileType=='png'){
		$im=imagecreatefrompng($file);
	}
	if(empty($savePath))
	$savePath=$file;
	$width=imagesx($im);
	$height=imagesy($im);
	if($width<$thumbWidth&&$height<$thumbHeight){
		return array('status'=>3,'file'=>'','width'=>0,'height'=>0,'savePath'=>'','info'=>'图片尺寸小于生成缩略图的尺寸');
	}
	$ratio=$width/$height;//实际图象的比例
	$thumbRatio=$thumbWidth/$thumbHeight;//改变后的图象的比例
	if($isCut){//裁切处理
		if(function_exists('imagepng')&&(str_replace('.','',PHP_VERSION)>=512)){//针对php版本大于5.12参数变化后的处理情况
			$quality=9;
		}
		if($ratio>=$thumbRatio){//高度优先
			$newimg=imagecreatetruecolor($thumbWidth,$thumbHeight);
			imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,(($height)*$thumbRatio),$height);
			imagejpeg($newimg,$savePath,$quality);
		}
		if($ratio<$thumbRatio){//宽度优先
			$newimg=imagecreatetruecolor($thumbWidth,$thumbHeight);
			imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,$thumbHeight,$width,(($width)/$thumbRatio));
			imagejpeg($newimg,$savePath,$quality);
		}
	}else{//不裁切处理
		if($ratio>=$thumbRatio){
			$newimg=imagecreatetruecolor($thumbWidth,($thumbWidth)/$ratio);
			imagecopyresampled($newimg,$im,0,0,0,0,$thumbWidth,($thumbWidth)/$ratio,$width,$height);
			imagejpeg($newimg,$savePath,$quality);
		}
		if($ratio<$thumbRatio){
			$newimg=imagecreatetruecolor(($thumbHeight)*$ratio,$thumbHeight);
			imagecopyresampled($newimg,$im,0,0,0,0,($thumbHeight)*$ratio,$thumbHeight,$width,$height);
			imagejpeg($newimg,$savePath,$quality);
		}
	}
	ImageDestroy($im);//www.Alixixi.com/php-function/1007.html
	return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'缩略图片生成成功');
}
到此完成,大家可以试试。
关键字:php、函数、缩略图
分享到:

顶部 】 【 关闭
版权所有:佛山思海电脑网络有限公司 ©1998-2024 All Rights Reserved.
联系电话:(0757)22630313、22633833
中华人民共和国增值电信业务经营许可证: 粤B1.B2-20030321 备案号:粤B2-20030321-1
网站公安备案编号:44060602000007 交互式栏目专项备案编号:200303DD003  
察察 工商 网安 举报有奖  警警  手机打开网站