Using jquery Jcrop and PHP for screenshot function

Source: Internet
Author: User
Implement features with jquery Jcrop and PHP

The project uses an upload avatar function, need to do not refresh the image upload, and upload the image of the user requirements, no refresh upload I don't say, with the juploader, I believe we are not unfamiliar, focus on the Jcron and PHP configuration to achieve the interception of images of the function, well, Anyway First introduce the use of Jcron, I will not explain, we only see the most frequently used to the function:

$ (function () {$ (' #cropbox '). Jcrop ({aspectratio:1,onselect:updatecoords});});

The above is the control, to which picture, "Cropbox" is the ID of the IMG object you want to intercept, "Aspectratio" control and other proportional interception, "onSelect" value is a method name, the method that is called when selected

, the details of the parameters are explained as follows:

option Name value Type description default
Span style= "font-size:18px" >aspectratio decimal aspect ratio of w/h (e.g. 1 for square) n/a
Span style= "font-size:18px" >minsize array [w, H] minimum Width/height, use 0 for unbounded dimension n/a
Span style= "font-size:18px" >maxsize array [w, H] maximum Width/height, use 0 for unbounded dimension n/a
Span style= "font-size:18px" >setselect array [x, Y, X2, y2] set an initial selection area n/a
Span style= "font-size:18px" >bgcolor color value set color of background container ' black '
Bgopacity decimal 0-1 Opacity of outer image when cropping .6
callback method When selected

function Updatecoords (c) {$ (' #x '). Val (c.x), $ (' #y '). Val (C.Y); $ (' #w '). Val (C.W); $ (' #h '). Val (c.h);};

With this method, you can update the coordinate information in the hidden field by hiding the field to the background.


OK, to this, the front desk has been over, we look behind the PHP code.

Background PHP is mainly based on the foreground of the coordinates transferred, the original image interception, support Jpg,png, and GIF three picture format, of course, you can extend him, so that he supports more image format.

Class Img_shot{private $filename;p rivate $ext;p rivate $x;p rivate $y;p rivate $x 1;private $y 1;private $width = 124;private $height = 124;private $jpeg _quality = 90;/** * constructor * * */public function __construct () {log_message (' Debug ', "Img_shot Cla SS Initialized ");}  /** * Initialize Object * @param filename source file path quanming * @param width of width * @param height of Height * @param x Axis 1 * @param y ordinate 1 * @param x1 Horizontal 1 * @param y1 Horizontal 2 * */public function Initialize ($filename, $x, $y, $x 1, $y 1) {if (file_exists ($filename)) {$this filename = $filename; $pathinfo = PathInfo ($filename); $this->ext = $pathinfo [' extension '];} else{$e = new Exception (' The file is not exists! ', 1050); throw $e;} $this->x = $x; $this->y = $y; $this->x1 = $x 1; $this->y1 = $y 1;} /** * Generate * Depending on the format of the picture, generate different */public function generate_shot () {switch ($this->ext) {case ' jpg ': return $this->generate_ JPG (); Break;case ' png ': Return $this->generate_png (); Break;case ' gif ': Return $this->generate_gif (); break; Default:return false;}} /** * Gets the generated filename * */private function Get_shot_name () {$pathinfo = PathInfo ($this->filename); $fileinfo = Explode ('. '), $pathinfo [' BaseName ']); $filename = $fileinfo [0]. ' _small. ' $this->ext;return $pathinfo [' dirname ']. '/'. $filename;} /** * Generate JPG format picture * */private function generate_jpg () {$shot _name = $this->get_shot_name (); $img _r = Imagecreatefromjpeg ($this->filename); $dst _r = Imagecreatetruecolor ($this->width, $this->height); imagecopyresampled ($dst _r,$ img_r,0,0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); imagejpeg ($dst _r , $shot _name, $this->jpeg_quality); return $shot _name;} /** * Generate picture in GIF format * */private function generate_gif () {$shot _name = $this->get_shot_name (); $img _r = Imagecreatefromgif ( $this->filename); $dst _r = Imagecreatetruecolor ($this->width, $this->height); imagecopyresampled ($dst _r,$ img_r,0,0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); Imagegif ($dst _r, $shot _name); return $shot _name;} /** * GeneratePicture in PNG format * */private function generate_png () {$shot _name = $this->get_shot_name (); $img _r = Imagecreatefrompng ($this- >filename); $dst _r = Imagecreatetruecolor ($this->width, $this->height); imagecopyresampled ($dst _r, $img _r, 0,0, $this->x, $this->y, $this->width, $this->height, $this->x1, $this->y1); Imagepng ($dst _r, $shot _ name); return $shot _name;}}

After receiving the coordinate information of the foreground, you can instantiate the class, use it to generate the picture, and return the name of the generated picture.


After you have finished cutting the diagram:


  • 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.