php multifunctional picture processing class sharing of php picture scaling class
<?php /** * Basic image processing, used to complete the image indentation, watermark add * When the watermark image exceeds the size of the target image, the watermark image can be shrunk automatically to fit the target image * The watermark can be set to merge with the background *//* Method of use : Automatic cutting : The program will cut the largest square in the middle according to the size of the image, and then shorten it according to the target size $t--->setSrcImg("img/test.jpg"); $t->setCutType(1);// this 1 A sentence is OK the $t->setDstImg("tmp/new_test.jpg"); $t->createImg(60,60); Manual cutting : The program fetches the graph from the source graph at the specified location $t->setSrcImg("img/test.jpg"); $t->setCutType(2);// Indicates manual cutting $t->setSrcCutPosition(100, 100);// Source graph starting point coordinates $t->setRectangleCut(300, 200);// Cutting size $t->setDstImg("tmp/new_test.jpg"); $t->createImg(300,200); */class ThumbHandler { var $dst_img;// The target file var $h_src; // Image resource handle var $h_dst;// It handles var $h_mask;// Handle to the watermark var $img_create_quality = 100;// Image Generation Quality var $img_display_quality = 80;// Picture display quality , The default is 75 var $img_scale = 0;// Picture scaling var $src_w = 0;// The original width var $src_h = 0;// The original height var $dst_w = 0;// Total width of the new image var $dst_h = 0;// Total height in new drawing var $fill_w;// Padding width var $fill_h;// Fill figure height var $copy_w;// Copy width var $copy_h;// Copy graph height var $src_x = 0;// The original drawing draws the starting abscissa var $src_y = 0;// The original drawing draws the starting y-coordinate var $start_x;// The new figure draws the starting abscissa var $start_y;// The new graph is drawn at the starting y-coordinate var $mask_word;// Watermark text var $mask_img;// The watermark image var $mask_pos_x = 0;// Watermark abscissa var $mask_pos_y = 0;// Ordinate of watermark var $mask_offset_x = 5;// Watermark lateral migration var $mask_offset_y = 5;// Watermark vertical migration var $font_w;// Watermark font width var $font_h;// Watermark font height var $mask_w;// Watermark is wide var $mask_h;// The watermark is high var $mask_font_color = "#ffffff";// Watermark text color var $mask_font = 2;// Watermark font var $font_size;// size var $mask_position = 0;// The watermark position var $mask_img_pct = 50;// Image merge degree , The higher the value, the lower the merge procedure var $mask_txt_pct = 50;// Degree of text consolidation , The smaller the value, the lower the merge procedure var $img_border_size = 0;// Picture border size var $img_border_color;// Image Border Color var $_flip_x=0;// Number of horizontal flips var $_flip_y=0;// Number of vertical flips var $cut_type=0;// Shear type var $img_type;// The file type // File type definition , And pointed out the output picture function var $all_type = array( "jpg" => array("output"=>"imagejpeg"), "gif" => array("output"=>"imagegif"), "png" => array("output"=>"imagepng"), "wbmp" => array("output"=>"image2wbmp"), "jpeg" => array("output"=>"imagejpeg")); /** * The constructor */ function ThumbHandler() { $this->mask_font_color = "#ffffff"; $this->font = 2; $this->font_size = 12; } /** * Gets the width of the image */ function getImgWidth($src) { return imagesx($src); } /** * Get the height of the picture */ function getImgHeight($src) { return imagesy($src); } /** * Set the image generation path * * @param string $src_img Image Generation Path */ function setSrcImg($src_img, $img_type=null) { if(!file_exists($src_img)) { die(" Image doesn't exist "); } if(!empty($img_type)) { $this->img_type = $img_type; } else { $this->img_type = $this->_getImgType($src_img); } $this->_checkValid($this->img_type); // file_get_contents Function requirements php version >4.3.0 $src = ''; if(function_exists("file_get_contents")) { $src = file_get_contents($src_img); } else { $handle = fopen ($src_img, "r"); while (!feof ($handle)) { $src .= fgets($fd, 4096); } fclose ($handle); } if(empty($src)) { die(" Image source is empty "); } $this->h_src = @ImageCreateFromString($src); $this->src_w = $this->getImgWidth($this->h_src); $this->src_h = $this->getImgHeight($this->h_src); } /** * Set the image generation path * * @param string $dst_img Image Generation Path */ function setDstImg($dst_img) { $arr = explode('/',$dst_img); $last = array_pop($arr); $path = implode('/',$arr); $this->_mkdirs($path); $this->dst_img = $dst_img; } /** * Sets the display quality of the image * * @param string $n The quality of */ function setImgDisplayQuality($n) { $this->img_display_quality = (int)$n; } /** * Sets the generated quality of the image * * @param string $n The quality of */ function setImgCreateQuality($n) { $this->img_create_quality = (int)$n; } /** * Set Text Watermark * * @param string $word Watermark text * @param integer $font Watermark font * @param string $color Watermark font color */ function setMaskWord($word) { $this->mask_word .= $word; } /** * Set the font color * * @param string $color The font color */ function setMaskFontColor($color="#ffffff") { $this->mask_font_color = $color; } /** * Set Watermark Font * * @param string|integer $font The font */ function setMaskFont($font=2) { if(!is_numeric($font) && !file_exists($font)) { die(" Font file does not exist "); } $this->font = $font; } /** * Sets the font size of text , Only the truetype Font effective */ function setMaskFontSize($size = "12") { $this->font_size = $size; } /** * Set Image Watermark * * @param string $img Watermark image source */ function setMaskImg($img) { $this->mask_img = $img; } /** * Set the watermark to horizontal offset * * @param integer $x Lateral offset */ function setMaskOffsetX($x) { $this->mask_offset_x = (int)$x; } /** * Set the watermark vertical offset * * @param integer $y Longitudinal offset */ function setMaskOffsetY($y) { $this->mask_offset_y = (int)$y; } /** * Specify watermark position * * @param integer $position location ,1: Upper left ,2: The lower left ,3: The upper right ,0/4: The lower right */ function setMaskPosition($position=0) { $this->mask_position = (int)$position; } /** * Sets the degree of image merging * * @param integer $n Degree of consolidation */ function setMaskImgPct($n) { $this->mask_img_pct = (int)$n; } /** * Sets the level of text merge * * @param integer $n Degree of consolidation */ function setMaskTxtPct($n) { $this->mask_txt_pct = (int)$n; } /** * Sets the thumbnail border * * @param ( type ) ( Parameter names ) ( describe ) */ function setDstImgBorder($size=1, $color="#000000") { $this->img_border_size = (int)$size; $this->img_border_color = $color; } /** * Flip horizontal */ function flipH() { $this->_flip_x++; } /** * Flip vertical */ function flipV() { $this->_flip_y++; } /** * Set clipping type * * @param ( type ) ( Parameter names ) ( describe ) */ function setCutType($type) { $this->cut_type = (int)$type; } /** * Set image clipping * * @param integer $width The rectangle shear */ function setRectangleCut($width, $height) { $this->fill_w = (int)$width; $this->fill_h = (int)$height; } /** * Sets the starting shear coordinate point of the source graph * * @param ( type ) ( Parameter names ) ( describe ) */ function setSrcCutPosition($x, $y) { $this->src_x = (int)$x; $this->src_y = (int)$y; } /** * Create a picture , The main function * @param integer $a When the lack of the first 2 Parameter, this parameter is used as a percentage, * Otherwise as the width value * @param integer $b The height of the image after scaling */ function createImg($a, $b=null) { $num = func_num_args(); if(1 == $num) { $r = (int)$a; if($r < 1) { die(" The picture zooming ratio should not be less than 1"); } $this->img_scale = $r; $this->_setNewImgSize($r); } if(2 == $num) { $w = (int)$a; $h = (int)$b; if(0 == $w) { die(" The target width cannot be 0"); } if(0 == $h) { die(" The target height cannot be 0"); } $this->_setNewImgSize($w, $h); } if($this->_flip_x%2!=0) { $this->_flipH($this->h_src); } if($this->_flip_y%2!=0) { $this->_flipV($this->h_src); } $this->_createMask(); $this->_output(); // The release of if(imagedestroy($this->h_src) && imagedestroy($this->h_dst)) { Return true; } else { Return false; } } /** * To generate the watermark , Two methods of generating watermark text and watermark image are called */ function _createMask() { if($this->mask_word) { // Get font information $this->_setFontInfo(); if($this->_isFull()) { die(" Watermark text is too large "); } else { $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// Fill background color $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); $this->_createMaskWord($this->h_dst); } } if($this->mask_img) { $this->_loadMaskImg();// When loading, get the width and height if($this->_isFull()) { // The watermark is generated on the original image and then copied $this->_createMaskImg($this->h_src); $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// Fill background color $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->start_y, $this->copy_w, $this->copy_h); } else { // Create a new diagram and copy it $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// Fill background color $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); $this->_createMaskImg($this->h_dst); } } if(empty($this->mask_word) && empty($this->mask_img)) { $this->h_dst = imagecreatetruecolor($this->dst_w, $this->dst_h); $white = ImageColorAllocate($this->h_dst,255,255,255); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$white);// Fill background color $this->_drawBorder(); imagecopyresampled( $this->h_dst, $this->h_src, $this->start_x, $this->start_y, $this->src_x, $this->src_y, $this->fill_w, $this->fill_h, $this->copy_w, $this->copy_h); } } /** * Picture frame */ function _drawBorder() { if(!empty($this->img_border_size)) { $c = $this->_parseColor($this->img_border_color); $color = ImageColorAllocate($this->h_src,$c[0], $c[1], $c[2]); imagefilledrectangle($this->h_dst,0,0,$this->dst_w,$this->dst_h,$color);// Fill background color } } /** * Generate watermark text */ function _createMaskWord($src) { $this->_countMaskPos(); $this->_checkMaskValid(); $c = $this->_parseColor($this->mask_font_color); $color = imagecolorallocatealpha($src, $c[0], $c[1], $c[2], $this->mask_txt_pct); if(is_numeric($this->font)) { imagestring($src, $this->font, $this->mask_pos_x, $this->mask_pos_y, $this->mask_word, $color); } else { imagettftext($src, $this->font_size, 0, $this->mask_pos_x, $this->mask_pos_y, $color, $this->font, $this->mask_word); } } /** * Generated watermark */ function _createMaskImg($src) { $this->_countMaskPos(); $this->_checkMaskValid(); imagecopymerge($src, $this->h_mask, $this->mask_pos_x ,$this->mask_pos_y, 0, 0, $this->mask_w, $this->mask_h, $this->mask_img_pct); imagedestroy($this->h_mask); } /** * Loading watermark */ function _loadMaskImg() { $mask_type = $this->_getImgType($this->mask_img); $this->_checkValid($mask_type); // file_get_contents Function requirements php version >4.3.0 $src = ''; if(function_exists("file_get_contents")) { $src = file_get_contents($this->mask_img); } else { $handle = fopen ($this->mask_img, "r"); while (!feof ($handle)) { $src .= fgets($fd, 4096); } fclose ($handle); } if(empty($this->mask_img)) { die(" The watermark image is empty "); } $this->h_mask = ImageCreateFromString($src); $this->mask_w = $this->getImgWidth($this->h_mask); $this->mask_h = $this->getImgHeight($this->h_mask); } /** * Image output */ function _output() { $img_type = $this->img_type; $func_name = $this->all_type[$img_type]['output']; if(function_exists($func_name)) { // Judge browser , if IE We don't send headers if(isset($_SERVER['HTTP_USER_AGENT'])) { $ua = strtoupper($_SERVER['HTTP_USER_AGENT']); if(!preg_match('/^.*MSIE.*\)$/i',$ua)) { header("Content-type:$img_type"); } } $func_name($this->h_dst, $this->dst_img, $this->img_display_quality); } else { Return false; } } /** * Analysis of color * * @param string $color 106 Hexadecimal color */ function _parseColor($color) { $arr = array(); for($ii=1; $ii<strlen function="" return="" this-="">_isFull()) { switch($this->mask_position) { case 1: // Upper left $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 2: // The lower left $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; case 3: // The upper right $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 4: // The lower right $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; default: // The default is to put the watermark in the bottom right , Offset specified pixel $this->mask_pos_x = $this->src_w - $this->mask_w - $this->mask_offset_x; $this->mask_pos_y = $this->src_h - $this->mask_h - $this->mask_offset_y; break; } } else { switch($this->mask_position) { case 1: // Upper left $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 2: // The lower left $this->mask_pos_x = $this->mask_offset_x + $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; case 3: // The upper right $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->mask_offset_y + $this->img_border_size; break; case 4: // The lower right $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; default: // The default is to put the watermark in the bottom right , Offset specified pixel $this->mask_pos_x = $this->dst_w - $this->mask_w - $this->mask_offset_x - $this->img_border_size; $this->mask_pos_y = $this->dst_h - $this->mask_h - $this->mask_offset_y - $this->img_border_size; break; } } } /** * Set font information */ function _setFontInfo() { if(is_numeric($this->font)) { $this->font_w = imagefontwidth($this->font); $this->font_h = imagefontheight($this->font); // Calculate the width and height of the watermark font $word_length = strlen($this->mask_word); $this->mask_w = $this->font_w*$word_length; $this->mask_h = $this->font_h; } else { $arr = imagettfbbox ($this->font_size,0, $this->font,$this->mask_word); $this->mask_w = abs($arr[0] - $arr[2]); $this->mask_h = abs($arr[7] - $arr[1]); } } /** * Sets the new diagram size * * @param integer $img_w The target width * @param integer $img_h Target height */ function _setNewImgSize($img_w, $img_h=null) { $num = func_num_args(); if(1 == $num) { $this->img_scale = $img_w;// Width as scale $this->fill_w = round($this->src_w * $this->img_scale / 100) - $this->img_border_size*2; $this->fill_h = round($this->src_h * $this->img_scale / 100) - $this->img_border_size*2; // Source file starting coordinates $this->src_x = 0; $this->src_y = 0; $this->copy_w = $this->src_w; $this->copy_h = $this->src_h; // The target size $this->dst_w = $this->fill_w + $this->img_border_size*2; $this->dst_h = $this->fill_h + $this->img_border_size*2; } if(2 == $num) { $fill_w = (int)$img_w - $this->img_border_size*2; $fill_h = (int)$img_h - $this->img_border_size*2; if($fill_w < 0 || $fill_h < 0) { die(" The image border is too large. It exceeds the width of the image "); }