>  기사  >  php教程  >  PHP从中间裁图最简单的思路

PHP从中间裁图最简单的思路

WBOY
WBOY원래의
2016-06-06 20:09:13926검색

PHP裁剪图片,一般是 imagecopyresampled() 函数,默认是从左上角开始切,然后看了一下网上从中间裁图的代码,都特别复杂,其实不用这么麻烦,只要定义一下imagecopyresampled里面那两个横纵坐标的string就行了 本文代码效果: 从三分之一宽度开始裁切, 定宽200, 高

PHP裁剪图片,一般是imagecopyresampled()函数,默认是从左上角开始切,然后看了一下网上从中间裁图的代码,都特别复杂,其实不用这么麻烦,只要定义一下imagecopyresampled里面那两个横纵坐标的string就行了
本文代码效果: 从三分之一宽度开始裁切, 定宽200, 高度不变, (如果要从高宽各一定比例的地方开始裁, 参照此法)

<?php $filename = '1.jpg';
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename); //get the size of old img
$center_width = floor( $width/3);  // cut from 1/3 width
$new_width = '200'; //width of result img
$new_height = $height; //height of result img, here we keep the same height
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, $center_width, 0, $new_width, $height, $new_width, $height);
imagejpeg($image_p, null, 100); 
?>

cut_img_from_center
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.