Home>

Recently doing an open source project for dragging verification codes on web pages,Need to generate movable color blocks corresponding to pictures on the server side,But the online resources are all thumbnails.Scaling the entire picture,So do it yourself,Completed the tool for cropping small pieces of pictures

<?php
namespace app \ libs;
/**
* 2016-01-07 15:54:58
* lixiaoyu
*
* mode 1:forced cropping,Generate pictures strictly as needed,Under-magnification,More than cropping,Pictures are always full
* mode 2:similar to 1, but if it is not enough, no zoom will produce filler,Can be eliminated with png.
* mode 3:only zoom, no crop, retain all picture information,Will produce fillers,* mode 4:only zoom, no crop, retain all picture information,The generated image size is the actual size of the effective information of the final scaled image.
Does not generate filler
* The default filler is white,If i want to make the padding transparent pixels,Please use savealpha () method instead of saveimage () method
* /
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 __construct ($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->gts=imagecreatefromgif ($this->gtsrc_file);
$this->ext="gif";
break;
default:
exit ();
}
}
function saveimage ($filename="") {
$this->dst_file=$filename?$filename:$this-&d;dst_file;
switch ($this->src_type) {
case imagetype_jpeg:
imagejpeg ($this->dimage, $this->dst_file, 100);
break;
case imagetype_png:
imagepng ($this-&d;dimage, $this-&d;dst_file);
break;
case imagetype_gif:
imagegif ($this-&d;dimage, $this-&d;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-&d;dimage);
break;
case imagetype_gif:
header ("content-type:image/gif");
imagegif ($this-&d;dimage);
break;
default:
break;
}
}
function savealpha ($filename="") {
$this->dst_file=$filename?$filename. ".png":$this->dst_file. ". png";
imagesavealpha ($this->dimage, true);
imagepng ($this-&d;dimage, $this-&d;dst_file);
}
function outalpha () {
imagesavealpha ($this->dimage, true);
header ("content-type:image/png");
imagepng ($this-&d;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-&d;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-&s;src_width)/2);
$dst_y=(int) (abs ($dst_height- $this-&s;src_height)/2);
imagecopy ($this-&d;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-&d;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
/**
*
* Cutting method
* 2016-01-07 15:05:44
* lixiaoyu
*
* @param $dst_width target length
* @param $dst_height target height
* @param $dst_x The distance between the cropped part and the left side of the original image
* @param $dst_y The distance between the cropped part and the right side of the original image
* @param int $mode mode
* @param string $dst_file destination file path
* /
function cut ($dst_width, $dst_height, $dst_x, $dst_y, $dst_file="")
{
if ($dst_file) $this->dst_file=$dst_file;//Set the destination file location
$this->dimage=imagecreatetruecolor ($dst_width, $dst_height);//Canvas with the size of the target file created
$bg=imagecolorallocatealpha ($this->dimage, 255, 255, 255, 127);//Assign color to canvas
imagefill ($this->dimage, 0, 0, $bg);//Fill the image with color
imagecolortransparent ($this->dimage, $bg);//The background is defined as a transparent color
$ratio_w=1.0 * $dst_width/$this->src_width;//The ratio of horizontal scaling
$ratio_h=1.0 * $dst_height/$this->src_height;//scale of vertical scaling
//var_dump ($this);
//do not scale,Crop images directly
$ratio=1.0;
$tmp_w=(int) ($dst_width/$ratio);
$tmp_h=(int) ($dst_height/$ratio);
$tmp_img=imagecreatetruecolor ($dst_width, $dst_height);//Create a temporarily saved canvas
imagecopy ($tmp_img, $this->simage, 0,0, $dst_x, $dst_y, $dst_width, $dst_height);//Copy a part of the image,Cut
imagecopyresampled ($this->dimage, $tmp_img, 0,0,0,0, $dst_width, $dst_height, $tmp_w, $tmp_h);//put the temporarily cached image,Put in the target file
imagedestroy ($tmp_img);
}
}
?>

use

Crop image

$ic=new imagecrop ($pathtofile, "./pic/aftercrop" .time (). ". jpg");
$ic->cut (40,30,120,130);
$ic->saveimage ();
//$ic->savealpha ();save the padding into transparent pixels
$ic->destory ();

Achieve effect

Original image

Cropped image

Scale image

Original image

Thumbnail

This article focuses on using the image processing functions imagecopy and imagecopyresampled

bool imagecopy (resource dstim, resourcesrc_im, int dstx, intdst_y, int srcx, intsrc_y, int srcw, intsrc_h)

Copy the coordinates in the src_im image starting from src_x, src_y, a width of src_w, and a height of src_h to the dst_im image at the coordinates of dst_x and dst_y.

php
  • Previous PHP use strstr () function to get all characters after the specified string
  • Next Android development art exploration study notes (7)