PHP image cropping thumbnail cropping source code and usage _ php instance

Source: Internet
Author: User
Tags image line imagecopy imagejpeg
This article mainly introduces PHP image cropping and cropping source code and usage related information. If you need it, please refer to the open-source project that recently launched the webpage drag-and-drop verification code, you need to generate a removable Color Block corresponding to the image on the server, but the resources on the Internet are thumbnails, scaling the entire image, so you have done it yourself and completed the tool to crop small pieces of the image.

<? Phpnamespace App \ Libs;/*** 15:54:58 * Lixiaoyu ** mode 1: similar to 1, but if it is insufficient, it will generate a fill white if it is not zoomed in. You can use png to remove it. * Mode 3: only scaling, no cropping, retain all image information, will generate a fill white, * mode 4: only scaling, no cropping, keep all image information, the size of the generated image is the actual size of the final scaled image's valid information. No padding is generated. * The default padding is white. To make the padding transparent, use SaveAlpha () the method replaces the 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 Se TSrcFile ($ 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); B Reak; 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_fil E = $ 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 cropif ($ 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, $ dst_width, $ dst_height, $ tmp_w, $ tmp_h); imagedestro Y ($ 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 = values ($ tmp_w, $ tmp_h); imagecopyresampled ($ tmp_img, $ this-> sImage, $ 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, $ src_x, $ src_y, $ dst_width, $ d St_height); imagedestroy ($ tmp_img);} break; case 2: // only smallif ($ 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, $ src_x, $ src_y, $ tmp_w, $ tmp_h ); imagecopyresampled ($ this-> dImage, $ tmp_img, 0, 0, 0, $ dst_width, $ dst_height, $ tmp_w, $ tmp_h); imagedestroy ($ t Mp_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 sizeif ($ 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 = values ($ tmp_w, $ tmp_h); imagecopyresampled ($ tmp_img, $ this-> sImage, $ 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 sizeif ($ ratio_w> 1 & $ ratio_h> 1) {$ this-> dImage = imagecreatetruecolor ($ this-> src_width, $ this-> src_height); imagecopy ($ this-> dImage, $ this-> sImage, 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, $ tmp_w, $ tmp_h, $ this-> src_width, $ this-> src_height);} break ;}} // end Crop/***** cropping Method * 15:05:44 * Lixiaoyu ** @ param $ dst_width destination length * @ param $ dst_height destination height * @ param $ dst_x cropping part and left side of the source Image distance * @ Param $ distance between the dst_y cropping part and the right side of the source image * @ param int $ 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 target file location $ this-> dImage = imagecreatetruecolor ($ dst_width, $ dst_height ); // create a canvas of the target file size $ bg = imagecolorallocatealpha ($ this-> dImage, 255,255,255,127); // assign the canvas color imagefill ($ this-> dImage, 0, 0, $ bg); // color the image Line-filling imagecolortransparent ($ this-> dImage, $ bg); // The background is defined as transparent. $ ratio_w = 1.0 * $ dst_width/$ this-> src_width; // scale horizontally $ ratio_h = 1.0 * $ dst_height/$ this-> src_height; // scale vertically // var_dump ($ this); // do not scale, crop an image 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 saved canvas imagecopy ($ tmp_img, $ this-> sImage, $ Dst_x, $ dst_y, $ dst_width, $ dst_height); // copy a part of the image to crop imagecopyresampled ($ this-> dImage, $ tmp_img, $ dst_width, $ dst_height, $ tmp_w, $ tmp_h); // put the image temporarily cached in the target file imagedestroy ($ tmp_img); }}?>

Use

Crop Images

$ Ic = new ImageCrop ($ pathToFile ,'. /pic/afterCrop'.time().'.jpg '); $ ic-> Cut (120,130,); $ ic-> SaveImage (); // $ ic-> SaveAlpha (); convert the padding into transparent pixels and save $ ic-> destory ();

Effect

Source image


Cropped Graph

Zoom Image

Source image

Thumbnail

This article focuses on the use of 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 of the src_im image from src_x, src_y, src_w in width, and src_h in height to the position where the coordinates of dst_im are dst_x and dst_y.

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.