PHP从中间裁图最简单的思路
Jun 06, 2016 pm 08:09 PMPHP裁剪图片,一般是 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 is coded by xiaohudie. Any posts here is one hundred percent original, so please keep my link so as not to hurt this pretty girl

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
