PHP cropping pictures

Source: Internet
Author: User
Tags imagecopy imagejpeg

GD Library to open

<?php


/**
* Author:smallchicken
* time:2009 June 8 16:46:05
* Mode 1: Forced cropping, generate images in strict accordance with the need, not enlarge, over cropping, the picture is always covered
* Mode 2: and 1 similar, but not when the General Assembly produces filler, can be removed with PNG.
* Mode 3: Only zoom, not crop, retain all picture information, will produce filler,
* Mode 4: Only zoom, not crop, keep all picture information, generate picture size for final scaled picture effective information of actual size, do not produce filler
* The default filler is white, if you want to make the filler into transparent pixels, use the Savealpha () method instead of the SaveImage () method
*
* Call Method:
*/
$ic =new imagecrop (' a.jpg ', ' aftercrop.jpg ');
$ic->crop (480,600,1);
$ic->saveimage ();
$ic->savealpha (); Turn filler into transparent pixel save
$ic->destory ();


Class imagecrop{

var $sImage;
var $dImage;
var $src _file;
var $dst _file;
var $src _width;
var $src _height;
var $src _ext;
var $src _type;

function Imagecrop ($src _file, $dst _file= ") {
$this->src_file= $src _file;
$this->dst_file= $DST _file;
$this->loadimage ();
}

function Setsrcfile ($src _file) {
$this->src_file= $src _file;
}

function Setdstfile ($dst _file) {
$this->dst_file= $DST _file;
}

function LoadImage () {
List ($this->src_width, $this->src_height, $this->src_type) = getimagesize ($this->src_file);
Switch ($this->src_type) {
Case IMAGETYPE_JPEG:
$this->simage=imagecreatefromjpeg ($this->src_file);
$this->ext= ' jpg ';
Break
Case Imagetype_png:
$this->simage=imagecreatefrompng ($this->src_file);
$this->ext= ' png ';
Break
Case Imagetype_gif:
$this->simage=imagecreatefromgif ($this->src_file);
$this->ext= ' gif ';
Break
Default
Exit ();
}
}

function SaveImage ($fileName = ") {
$this->dst_file= $fileName? $fileName: $this->dst_file;
Switch ($this->src_type) {
Case IMAGETYPE_JPEG:
Imagejpeg ($this->dimage, $this->dst_file,100);
Break
Case Imagetype_png:
Imagepng ($this->dimage, $this->dst_file);
Break
Case Imagetype_gif:
Imagegif ($this->dimage, $this->dst_file);
Break
Default
Break
}
}

function Outimage () {
Switch ($this->src_type) {
Case IMAGETYPE_JPEG:
Header (' Content-type:image/jpeg ');
Imagejpeg ($this->dimage);
Break
Case Imagetype_png:
Header (' content-type:image/png ');
Imagepng ($this->dimage);
Break
Case Imagetype_gif:
Header (' content-type:image/gif ');
Imagegif ($this->dimage);
Break
Default
Break
}
}

function Savealpha ($fileName = ") {
$this->dst_file= $fileName? $fileName. '. png ': $this->dst_file '. PNG ';
Imagesavealpha ($this->dimage, true);
Imagepng ($this->dimage, $this->dst_file);
}

function Outalpha () {
Imagesavealpha ($this->dimage, true);
Header (' content-type:image/png ');
Imagepng ($this->dimage);
}

function Destory () {
Imagedestroy ($this->simage);
Imagedestroy ($this->dimage);
}

function Crop ($dst _width, $dst _height, $mode =1, $dst _file= ") {
if ($dst _file) $this->dst_file= $dst _file;
$this->dimage = Imagecreatetruecolor ($dst _width, $dst _height);

$BG = Imagecolorallocatealpha ($this->dimage,255,255,255,127);
Imagefill ($this->dimage, 0, 0, $BG);
Imagecolortransparent ($this->dimage, $BG);

$ratio _w=1.0 * $DST _width/$this->src_width;
$ratio _h=1.0 * $DST _height/$this->src_height;
$ratio = 1.0;
Switch ($mode) {
Case 1://Always crop
if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1)) {
$ratio = $ratio _w < $ratio _h? $ratio _h: $ratio _w;
$tmp _w = (int) ($DST _width/$ratio);
$tmp _h = (int) ($DST _height/$ratio);
$tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h);
$SRC _x = (int) (($this->src_width-$tmp _w)/2);
$SRC _y = (int) (($this->src_height-$tmp _h)/2);
Imagecopy ($tmp _img, $this->simage, 0,0, $src _x, $src _y, $tmp _w, $tmp _h);
Imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h);
Imagedestroy ($tmp _img);
}else{
$ratio = $ratio _w < $ratio _h? $ratio _h: $ratio _w;
$tmp _w = (int) ($this->src_width * $ratio);
$tmp _h = (int) ($this->src_height * $ratio);
$tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h);
Imagecopyresampled ($tmp _img, $this->simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_height);
$SRC _x = (int) ($tmp _w-$dst _width)/2;
$SRC _y = (int) ($tmp _h-$dst _height)/2;
Imagecopy ($this->dimage, $tmp _img, 0,0, $src _x, $src _y, $dst _width, $dst _height);
Imagedestroy ($tmp _img);
}
Break
Case 2://Only Small
if ($ratio _w < 1 && $ratio _h < 1) {
$ratio = $ratio _w < $ratio _h? $ratio _h: $ratio _w;
$tmp _w = (int) ($DST _width/$ratio);
$tmp _h = (int) ($DST _height/$ratio);
$tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h);
$SRC _x = (int) ($this->src_width-$tmp _w)/2;
$SRC _y = (int) ($this->src_height-$tmp _h)/2;
Imagecopy ($tmp _img, $this->simage, 0,0, $src _x, $src _y, $tmp _w, $tmp _h);
Imagecopyresampled ($this->dimage, $tmp _img,0,0,0,0, $dst _width, $dst _height, $tmp _w, $tmp _h);
Imagedestroy ($tmp _img);
}elseif ($ratio _w > 1 && $ratio _h > 1) {
$DST _x = (int) abs ($DST _width-$this->src_width)/2;
$DST _y = (int) abs ($DST _height-$this->src_height)/2;
Imagecopy ($this->dimage, $this->simage, $dst _x, $dst _y,0,0, $this->src_width, $this->src_height);
}else{
$src _x=0, $dst _x=0, $src _y=0; $dst _y=0;
if (($dst _width-$this->src_width) < 0) {
$SRC _x = (int) ($this->src_width-$dst _width)/2;
$DST _x = 0;
}else{
$src _x = 0;
$DST _x = (int) ($DST _width-$this->src_width)/2;
}

if (($dst _height-$this->src_height) < 0) {
$SRC _y = (int) ($this->src_height-$dst _height)/2;
$DST _y = 0;
}else{
$src _y = 0;
$DST _y = (int) ($DST _height-$this->src_height)/2;
}
Imagecopy ($this->dimage, $this->simage, $dst _x, $dst _y, $src _x, $src _y, $this->src_width, $this->src_ height);
}
Break
Case 3://Keep all image size and create need size
if ($ratio _w > 1 && $ratio _h > 1) {
$DST _x = (int) (ABS ($DST _width-$this->src_width)/2);
$DST _y = (int) (ABS ($DST _height-$this->src_height)/2);
Imagecopy ($this->dimage, $this->simage, $dst _x, $dst _y,0,0, $this->src_width, $this->src_height);
}else{
$ratio = $ratio _w > $ratio _h? $ratio _h: $ratio _w;
$tmp _w = (int) ($this->src_width * $ratio);
$tmp _h = (int) ($this->src_height * $ratio);
$tmp _img=imagecreatetruecolor ($tmp _w, $tmp _h);
Imagecopyresampled ($tmp _img, $this->simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_height);
$DST _x = (int) (ABS ($tmp _w-$DST _width)/2);
$DST _y = (int) (ABS ($tmp _h-$DST _height)/2);
Imagecopy ($this->dimage, $tmp _img, $dst _x, $dst _y,0,0, $tmp _w, $tmp _h);
Imagedestroy ($tmp _img);
}
Break
Case 4://Keep all image but create actually size
if ($ratio _w > 1 && $ratio _h > 1) {
$this->dimage = Imagecreatetruecolor ($this->src_width, $this->src_height);
Imagecopy ($this->dimage, $this->simage,0,0,0,0, $this->src_width, $this->src_height);
}else{
$ratio = $ratio _w > $ratio _h? $ratio _h: $ratio _w;
$tmp _w = (int) ($this->src_width * $ratio);
$tmp _h = (int) ($this->src_height * $ratio);
$this->dimage = Imagecreatetruecolor ($tmp _w, $tmp _h);
Imagecopyresampled ($this->dimage, $this->simage,0,0,0,0, $tmp _w, $tmp _h, $this->src_width, $this->src_ height);
}
Break
}
}//End Crop


}
?>

PHP cropping pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.