CI Framework encapsulation of common image processing methods (thumbnails, watermarks, rotations, uploads, etc.) _php instances

Source: Internet
Author: User
Tags codeigniter zend framework
This paper describes the common image processing methods of CI framework encapsulation. Share to everyone for your reference, as follows:

In fact, when the mobile phone, List diagram is best thumbnail, save the flow, this does not, and was moved to a hole, call a point on the stop, traffic owed to 90 blocks before the stop, I was drunk ...

Do not say nonsense, the following is the use of CI built-in processing of images of the library to write, little brother, the omission of the place please point out, thank you.

/*** Generate thumbnails * @param local path to $path original image * @return Null to create an original _thumb. File **/public function Dealthumb ($path) {$config [' imag    E_library '] = ' gd2 ';    $config [' source_image '] = $path;    $config [' create_thumb '] = TRUE;    The resulting thumbnail will be close to the width and height of the setting in the aspect ratio to maintain the horizontal and vertical proportions $config [' maintain_ratio '] = TRUE;    $config [' width '] = 80;    $config [' height '] = 80;    $this->load->library (' Image_lib ', $config);    $this->image_lib->resize (); $this->image_lib->clear ();}    /** processing image rotation */public function transroate ($path, $imgpath) {$this->load->library (' image_lib ');    (must) set the image library $config [' image_library '] = ' gd2 '; $newname = Time (). '    _rote.jpg ';    Sets the target name/path of the image $config [' new_image '] = $imgpath. $newname;    (must) Set the name/path of the original image $config [' source_image '] = $path;    Determines whether the new image generation is to be written to the hard disk or the dynamic presence $config [' dynamic_output '] = FALSE; Sets the quality of the image.    The higher the quality, the larger the image file $config [' quality '] = ' 90% ';    There are 5 rotation options counterclockwise 90 180 270 degrees VRT vertical rollover hor transverse flip $config [' rotation_angle '] = ' VRT '; $thisImage_lib->initialize ($config);      if (@ $this->image_lib->rotate ()) {$this->image_lib->clear ();    return $config [' New_image '];      }else{$this->image_lib->clear ();    Return ';    }}/*** processing 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 PHP GD library drawing $config [' image_library '] = ' gd2 ';    Source image Local Address $config [' source_image '] = $path;    Overlay text $config [' wm_text '] = ' Copyright 2015-friker ';    Overwrite type 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 '] = ' ff0000 ';    The distance from the top of the vertical direction $config [' wm_vrt_alignment '] = ' 20 ';    The distance from the left side of the horizontal direction $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 ';    }}/*** processing image upload * File Upload class upload file via foreground */public function UploadFile () {//File upload part//process file//$data = ';    $this->load->helper (' url ');    $formpic = key ($_files); The File Processing section if (false = = = Empty ($_files[$formpic] [' tmp_name '])) {//Set file upload path $upload [' upload_path '] = "./public/i      mg/";      Limit the type of file upload $upload [' allowed_types '] = ' jpeg|jpg|gif|png ';      Limit the size of file uploads $upload [' max_size '] = 2048;      Set the file upload path $upload [' file_name '] = date (' Ymdhis ', Time ()). Rand (10000, 99999);      Load file upload configuration information $this->load->library (' upload ', $upload);      Process file Upload $this->upload->do_upload ($formpic);      Returns file upload information $image = $this->upload->data (); /* ' file_name ' + string ' 2015071702051718388.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/p Ublic/img/2015071702051718388.jpg ' (length=53) ' raw_name ' = = String ' 2015071702051718388 ' (length=19) ' Orig _name ' = ' = String ' 2015071702051718388.jpg ' (length=23) ' client_name ' = ' = String ' u=415761610,1548338330&fm= 116&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 ' image_type ' = ' s Tring ' JPEG ' (length=4) ' image_size_str ' + string ' width= "146" height= "" (length=24) *///var_dum      P ($image);      Return 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 thumbnail name $pos = Strripos ($image [' file_name '], "."); $newname = substr ($image [' file_name '], 0, $pos). "      _thumb ". substr ($image [' file_name '], $pos);      if (file_exists ($image [' File_path ']. $newname)) {$thumbdata = $newname;    }}//$dirroot = $_server[' document_root '); $this->dealthumb ($dirroot. "    /public/img/". $data); Upload failed if (! $data) {echo json_encode (' status ' =>0, ' msg ' = ' = ') upload failed!    ")); }else{//Upload succeeded Echo json_encode (Array (' name ' = = $data, ' pic ' =>base_url (). "    public/img/". $data, ' picthumb ' = = ' $thumbdata = = '? $data: $thumbdata)); }}

Here is the basic HTML code for the front-end:

 
  
 
  
 
  
 
  
 
  
 
                (
 
  
   
  best size X 80 pixels
 
   )     

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP Excellent Development Framework Summary", "thinkphp Getting Started", " Summary of common methods of thinkphp, "Introduction to Zend Framework Frame", "Introduction to PHP Object-oriented Programming", "Introduction to Php+mysql Database Operation" and "PHP common database Operation Skills Summary"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

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