Home > Backend Development > PHP Tutorial > php生成图片缩略图方法收集

php生成图片缩略图方法收集

WBOY
Release: 2016-06-20 13:05:13
Original
1344 people have browsed it

php生成图片缩略图方法函数代码如下:

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

使用方法如下:

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

打印结果:

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,不生成新图片


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template