PHP function to generate image thumbnails_PHP tutorial

WBOY
Release: 2016-07-13 10:28:58
Original
941 people have browsed it

The specific usage is as follows:

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

Print results:

Array(
[status] => 0
[file] => 1.jpg
[width] => 200
[height] => 200
[savePath] => 2.jpg
[info] => Thumbnail image generated successfully
)

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

The function code is as follows:

<?php
/*
*php生成图片缩略图的函数
*www.phpernote.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.phpernote.com/php-function/1007.html
	return array('status'=>0,'file'=>$file,'width'=>$thumbWidth,'height'=>$thumbHeight,'savePath'=>$savePath,'info'=>'缩略图片生成成功');
}
Copy after login

Articles you may be interested in

  • PHP class method for generating image thumbnails
  • PHP generates continuous numeric (letter) array function range() analysis, PHP lottery program function
  • PHP generate random password function
  • php method to get a list of all pictures in the content and output it
  • php extract the birthday date from the ID number and verify it Whether it is a minor function
  • PHP program to generate histogram (statistical chart)
  • PHP to generate verification code function
  • The idea and implementation method of php generating short URL

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/779412.htmlTechArticleThe specific usage is as follows: $a=thumbImage('1.jpg',200,100,'2.jpg'); //Generate a thumbnail of 1.jpg with a width of 200 and a height of 100, and save it as 2.jpgprint_r($a); Print result: Array( [status] = 0 [...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!