Home >php教程 >PHP源码 >在中间部分截取指定大小图片

在中间部分截取指定大小图片

PHP中文网
PHP中文网Original
2016-05-25 17:01:111126browse

[PHP]代码  

function getcentreimg($src_ore,$src_thumb,$width,$height){
		 $img = getimagesize($src_ore);//载入图片的函数   得到图片的信息
		 switch($img[2]){//判断图片的类型
			  case 1:
			   $im=@imagecreatefromgif($src_ore);//载入图片,创建新图片
			  break;
			  case 2:
			   $im=@imagecreatefromjpeg($src_ore);
			  break;
			  case 3:
			   $im=@imagecreatefrompng($src_ore);
			  break;
		 }
		 $width_y=$img[0];
		 $height_y=$img[1];
		 if($width_y>$height_y){//如果宽大于高
			  $width_y_y=$height_y;
			  $height_y_y=$height_y;
			  $jq_x=($width_y-$height_y)/2;
			  $jq_y=0;
		 }else if($width_y<$height_y){//如果宽小于高
			  $height_y_y=$width_y;
			  $width_y_y=$width_y;
			  $jq_x=0;
			  $jq_y=($height_y-$width_y)/2;
		 }else if($width_y=$height_y){//如果宽小于高
			  $width_y_y=$width_y;
			  $height_y_y=$height_y;
			  $jq_x=0;
			  $jq_y=0;
		 }
		 $newim=imagecreatetruecolor($width,$height); //剪切图片第一步,建立新图像 x就是宽 ,y就是高//图片大小
		 imagecopyresampled($newim,$im,0,0,$jq_x,$jq_y,$width,$height,$width_y_y,$height_y_y);//这个函数不失真
		 imagejpeg($newim,$src_thumb);
	}
getcentreimg("C:\Users\Public\Pictures\Sample Pictures\c.jpg","C:\Users\Public\Pictures\Sample Pictures\cc.jpg",&#39;100&#39;,&#39;100&#39;);

                   

                   

Statement:
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