Common image processing methods encapsulated by CI framework (thumbnails, watermarks, rotating, uploading, etc.) _ php instance

Source: Internet
Author: User
Tags image processing library
This article mainly introduces common image processing methods encapsulated by the CI framework, involving thumbnails, watermarks, rotating, uploading, and other operations for images, for more information about the image processing methods encapsulated by the CI framework, see the examples in this article. We will share this with you for your reference. The details are as follows:

In fact, when the mobile phone end is used, it is better to use a thumbnail to save traffic. if this is not the case, it is moved into a trap. if the phone bill is signed for one minute, it will be stopped and the traffic will be stopped until 90 yuan is owed, i'm also drunk...

Let's not talk nonsense. The following is written by using the built-in image processing library of CI. The younger brother is not talented. please point out the omissions. thank you.

/*** Generate a thumbnail * @ param $ path local path of the source image * @ return null create an original image _ thumb. file extension **/public function dealthumb ($ path) {$ config ['image _ library '] = 'gd2 '; $ config ['Source _ image'] = $ path; $ config ['create _ thumb'] = TRUE; // The generated thumbnail will keep the aspect ratio in width and height close to the configured width and height $ config ['maintain _ ratio '] = TRUE; $ config ['width'] = 80; $ config ['height'] = 80; $ this-> load-> library ('image _ lib', $ config ); $ this-> image_lib-> resize (); $ This-> image_lib-> clear ();}/** process image rotation */public function transroate ($ path, $ imgpath) {$ this-> load-> library ('image _ lib'); // (required) set image library $ config ['image _ library'] = 'gd2 '; $ newname = time().'_rote.jpg '; // set the image target name/path $ config ['new _ image'] = $ imgpath. $ newname; // (required) specify the original image name/path $ config ['Source _ image'] = $ path; // determine whether to write the new image to the hard disk or whether it dynamically exists. $ config ['dynamic _ output'] = FALSE; // set the image quality. The higher the quality, the larger the image file $ config ['qc '] = '20160301 '; // There are 5 rotation options: 90 180 270 ° vrt vertical flip hor horizontal flip $ config ['rotation _ Ange'] = 'vrt '; $ this-> image_lib-> initialize ($ config); if (@ $ this-> image_lib-> rotate () {$ this-> image_lib-> clear (); return $ config ['new _ image'];} else {$ this-> image_lib-> clear (); return '';}} /*** process image watermark */public function overlay ($ path, $ imgpath) {$ this-> load-> library ('image _ lib '); $ newname = time (). '_Over.jpg '; // set the new image name $ config ['new _ image'] = $ imgpath. $ newname; // call the php gd library to plot $ config ['image _ library'] = 'gd2 '; // Local Address of the source image $ config ['Source _ image'] = $ path; // overwrite the text $ config ['wm _ text'] = 'copyright 2015-Friker '; // overwrite text/image $ config ['wm _ type'] = 'text '; // text font type // $ config ['wm _ font_path'] = 'C: \ Windows \ Fonts \ vrinda. ttf'; // font size $ config ['wm _ font_size '] = '16'; // font color $ config ['wm _ font_color'] = 'ff00 00'; // The vertical distance from the top to $ config ['wm _ vrt_alignment '] = '20 '; // the horizontal distance from the left end is $ config ['wm _ hor_alignment '] = 'center'; // padding $ config ['wm _ padding'] = '20 '; $ this-> image_lib-> initialize ($ config); if ($ this-> image_lib-> watermark () {$ this-> image_lib-> clear (); return $ config ['new _ image'];} else {$ this-> image_lib-> clear (); return '';}} /*** process Image uploads * file Upload class uploads a file through the foreground */public function uploadfile () {// Upload part of the file // process the file // $ Data = ''; $ this-> load-> helper ('URL'); $ formpic = key ($ _ FILES ); // if (false === empty ($ _ FILES [$ formpic] ['tmp _ name']) {// Set the file upload path $ upload ['upload _ path'] = ". /public/img/"; // restrict the file upload type $ upload ['allowed _ types '] = 'jpeg | jpg | gif | png '; // restrict the file upload size $ upload ['max _ size'] = 2048; // Set the file upload path $ upload ['File _ name'] = date ('ymdhis ', time ()). rand (10000,999 99); // load file upload configuration information $ this-> load-> library ('uplo Ad', $ upload); // process file upload $ this-> upload-> do_upload ($ formpic ); // return the file upload information $ image = $ this-> upload-> data ();/* 'File _ name' => string '505071702051718388.jpg '(length = 23) 'File _ type' => string 'image/jpeg '(length = 10) 'File _ path' => string 'E: /wamp/www/testci/public/img/'(length = 30) 'full _ path' => string 'E: /wamp/www/testci/public/img/2015071702051718388.jpg '(length = 53) 'raw _ name' => string' 20150717 02051718388 '(length = 19) 'orig _ name' => string '505071702051718388.jpg' (length = 23) 'Client _ name' => string 'u00000000761610,1548338330&fm=&gp=0.jpg '(length = 38) 'File _ ext '=> string '.jpg' (length = 4) 'File _ size' => float 3.74 'is _ image' => boolean true 'image _ width' => int 146 'image _ height' => int 220 'image _ type '=> string 'jpeg' (length = 4) 'image _ size_str '=> string 'width = "146" height = "220 "' (Length = 24) * // var_dump ($ image); // returns the file upload name $ data = $ image ['File _ name']; $ this-> dealthumb ($ image ['full _ path']); $ this-> overlay ($ image ['full _ path'], $ image ['File _ path']); $ this-> transroate ($ image ['full _ path'], $ image ['File _ path']); // $ thumbdata = ''; // Generate a thumbnail name $ pos = strripos ($ image ['File _ name'],". "); $ newname = substr ($ image ['File _ name'], 0, $ pos ). "_ thumb ". substr ($ image ['File _ name'], $ pos); if (fil E_exists ($ image ['File _ path']. $ newname) {$ thumbdata = $ newname; }}// $ dirroot = $ _ SERVER ['document _ root']; // $ this-> dealthumb ($ dirroot. "/public/img /". $ data); // if (! $ Data) {echo json_encode (array ('status' => 0, 'MSG '=> "Upload failed! ");} Else {// echo json_encode (array ('name' => $ data, 'Pic '=> base_url (). "public/img /". $ data, 'picthumb' => $ thumbdata = ''? $ Data: $ thumbdata ));}}

The following is the basic html code of the front-end:

 
 
 
 
 
 

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.