Image processing class (domestic edition)

Source: Internet
Author: User
: This article mainly introduces Image Processing (image processing). For more information about PHP tutorials, see.
 $ Info [0], 'height' => $ info [1], 'type' => image_type_to_extension ($ info [2], false ), 'Mime '=> $ info ['Mime']); // Obtain image information $ type = self: $ info ['type']; $ fun = "imagecreatefrom {$ type}"; self: $ image = $ fun ($ src );} /*** @ param int $ width $ height should be declared in the configuration file, cancel the creation and use of the parameter * @ return thumbnail image resource * thumbnail */public function thumb ($ width, $ height) {// New Town color image $ image_thumb = imagecreatetruecolor ($ width, $ height); # obtain the image width to height ratio $ src_m = self :$ info ['width']/self:: $ info ['height']; # ratio of space in the source file to $ dst_m = $ width/$ height; # The width to height in the thumbnail # The width of the source file image is N: 1, change the height if ($ src_m> $ dst_m) {$ cha_width = $ width; $ cha_height = ceil ($ width/$ src_m);} else {# the source file image is 1: the height of the N type remains unchanged. change the width $ cha_width = floor ($ height * $ src_m); $ cha_height = $ height ;} # reset the actual position of the thumbnail $ dst_x = ($ width-$ cha_width)/2; $ dst_y = ($ height-$ cha_height)/2; imagecopyresampled ($ image_thumb, self: $ image, $ dst_x, $ dst_y, 0, 0, $ cha_width, $ cha_height, self: $ info ['width'], self :: $ info ['height']); # generate a thumbnail self: $ image = $ image_thumb; // # display a thumbnail image // self: show (self :: $ image); # save the thumbnail self: save (self: getNewName (); // destroy the image imagedestroy ($ this-> image_thumb); # return the thumbnail name return self:: getNewName () ;}# watermark generation coordinate private static function setLocal ($ pos) {# determine the pos parameter, specify the coordinates for generating the corresponding watermark # record the watermark image in the config file $ conf ['Mark'] switch ($ pos) {case 1: $ x = 0; $ y = 0; break; case 2: default: $ x = self: $ info ['width']-50; $ y = self: $ info ['height']-50; # It shouldn't be 20. you should change it to the width and height of the watermark image.} return $ local = array ('x' => $ x, 'y' => $ y );} # Add a text watermark public function fontMark ($ content, $ font_url, $ size, $ angle) {# font color $ col = imagecolorallocatealpha (self ::$ image, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255), 20); # get the Watermark Output position coordinate $ local = self: setLocal (2); imagettftext (self: $ image, $ size, $ angle, $ local ['x'], $ local ['Y'], $ col, $ font_url, $ content); # display thumbnail image self :: show (self: $ image); # save the text watermark without adding the save path self: save (self: getNewName (); # return the watermark image name return self :: getNewName () ;}# add an image watermark public function imageMark ($ url, $ alpha) {$ info = getimagesize ($ url ); # get image information $ type = image_type_to_extension ($ info [2], false); $ fun = "imagecreatefrom {$ type}"; # get Watermark Output position coordinates $ local = self:: setLocal (2); $ water = $ fun ($ url); # watermark image imagecopymerge (self: $ image, $ water, $ local ['x'], $ local ['Y'], 0, 0, $ info [0], $ info [1], 30); # destroy the image watermark imagedestroy ($ water ); # display thumbnail image self: show (self: $ image); # save the image watermark without adding the save path self: save (self: getNewName ()); # return the watermark image name return self: getNewName ();} # Generate a random image name/*** @ return string to return a new name **/private static function getNewName () {# get a time $ str = time (); # obtain a random string $ string = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOASDFGHJKZXCVBNM1234567890"; for ($ I = 0; $ I <6; $ I ++) {$ str. = $ string [mt_rand (0, strlen ($ string)-1)];} return $ str. self: $ info ['type'] ;}# output the image private static function show () {header ("content-type :". self: $ info ['Mime ']); $ funs = "image ". self: $ info ['type']; $ funs (self: $ image) ;}# save the image to the hard drive private static function save ($ newname) {$ funs = "image ". self: $ info ['type']; $ funs (self: $ image, $ newname. ". ". self: $ info ['type']); # add the directory where the configuration file is generated} # destroy the image public function _ destruct () {imagedestroy (self :: $ image );}}

The above introduces the image processing class (domestic version), including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.