The following simple to write a picture processing class, features include: watermarks, thumbnails and so on. However, there are two ways to generate thumbnails: one is to compress the pictures directly proportionally, and the other is to cut and then compress the images first. In my opinion, the difference between compression and cropping compression is: and other examples of compression: to ensure that the picture of the width and length of reasonable, and the picture has integrity. However, the actual size is not guaranteed to meet the requirements. Clip compression: To ensure that the picture of the width and length of reasonable, the actual size can also be guaranteed. But picture integrity is not guaranteed. image.php
- /**
- *
- * Image Processing Class
- * @author Fc_lamp
- * @internal features include: watermark, thumbnail
- */
- Class IMG
- {
- Picture format
- Private $exts = array (' jpg ', ' jpeg ', ' gif ', ' BMP ', ' PNG ');
- /**
- *
- *
- * @throws Exception
- */
- Public Function __construct ()
- {
- if (! function_exists (' Gd_info '))
- {
- throw new Exception (' Load GD library failed! ' );
- }
- }
- /**
- *
- * Clipping compression
- * @param $src _img Pictures
- * @param $save _img After the image is created
- * @param $option parameter options, including: $maxwidth wide $maxheight High
- * Array (' width ' =>xx, ' height ' =>xxx)
- * @internal
- * Our general compressed image method, the image generated when the picture is too long or too wide
- * Will be "squashed", for this should be the first cut after the proportional compression method
- */
- Public Function thumb_img ($src _img, $save _img = ", $option)
- {
- if (Empty ($option [' width ']) or empty ($option [' height ']))
- {
- Return Array (' flag ' = False, ' msg ' = ' = ' original length and width cannot be less than 0 ');
- }
- $org _ext = $this->is_img ($src _img);
- if (! $org _ext [' flag '])
- {
- return $org _ext;
- }
- If there is a save path, determine if the path is correct
- if (! empty ($save _img))
- {
- $f = $this->check_dir ($save _img);
- if (! $f [' flag '])
- {
- return $f;
- }
- }
- Get the appropriate method
- $org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
- Get the original size
- $source = $org _funcs [' Create_func '] ($src _img);
- $src _w = Imagesx ($source);
- $src _h = Imagesy ($source);
- Adjust the original image (keep the picture in the original shape to crop the image)
- $DST _scale = $option [' height ']/$option [' width ']; Target image aspect ratio
- $src _scale = $src _h/$src _w; Original aspect ratio
- if ($src _scale >= $dst _scale)
- {//Too high
- $w = Intval ($src _w);
- $h = intval ($dst _scale * $w);
- $x = 0;
- $y = ($src _h-$h)/3;
- } else
- {//Over width
- $h = Intval ($src _h);
- $w = Intval ($h/$DST _scale);
- $x = ($src _w-$w)/2;
- $y = 0;
- }
- Cutting
- $croped = Imagecreatetruecolor ($w, $h);
- Imagecopy ($croped, $source, 0, 0, $x, $y, $src _w, $src _h);
- Scaling
- $scale = $option [' width ']/$w;
- $target = Imagecreatetruecolor ($option [' width '], $option [' height ']);
- $final _w = intval ($w * $scale);
- $final _h = intval ($h * $scale);
- Imagecopyresampled ($target, $croped, 0, 0, 0, 0, $final _w, $final _h, $w, $h);
- Imagedestroy ($croped);
- Output (Save) picture
- if (! empty ($save _img))
- {
- $org _funcs [' Save_func '] ($target, $save _img);
- } else
- {
- Header ($org _funcs [' header ']);
- $org _funcs [' Save_func '] ($target);
- }
- Imagedestroy ($target);
- Return Array (' flag ' = = True, ' msg ' = = ');
- }
- /**
- *
- * Equal scaled image
- * @param $src _img original picture
- * @param where $save _img need to be saved
- * @param $option parameter Set Array (' Width ' =>xx, ' height ' =>xxx)
- *
- */
- function Resize_image ($src _img, $save _img = ", $option)
- {
- $org _ext = $this->is_img ($src _img);
- if (! $org _ext [' flag '])
- {
- return $org _ext;
- }
- If there is a save path, determine if the path is correct
- if (! empty ($save _img))
- {
- $f = $this->check_dir ($save _img);
- if (! $f [' flag '])
- {
- return $f;
- }
- }
- Get the appropriate method
- $org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
- Get the original size
- $source = $org _funcs [' Create_func '] ($src _img);
- $src _w = Imagesx ($source);
- $src _h = Imagesy ($source);
- if ($option [' width '] && $src _w > $option [' width ']) | | ($option [' height '] && $src _h > $option [' Height '])
- {
- if ($option [' width '] && $src _w > $option [' Width ']
- {
- $widthratio = $option [' width ']/$src _w;
- $resizewidth _tag = true;
- }
- if ($option [' height '] && $src _h > $option [' Height ']
- {
- $heightratio = $option [' height ']/$src _h;
- $resizeheight _tag = true;
- }
- if ($resizewidth _tag && $resizeheight _tag)
- {
- if ($widthratio < $heightratio)
- $ratio = $widthratio;
- Else
- $ratio = $heightratio;
- }
- if ($resizewidth _tag &&! $resizeheight _tag)
- $ratio = $widthratio;
- if ($resizeheight _tag &&! $resizewidth _tag)
- $ratio = $heightratio;
- $newwidth = $src _w * $ratio;
- $newheight = $src _h * $ratio;
- if (function_exists ("imagecopyresampled"))
- {
- $newim = Imagecreatetruecolor ($newwidth, $newheight);
- Imagecopyresampled ($newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src _w, $src _h);
- } else
- {
- $newim = Imagecreate ($newwidth, $newheight);
- Imagecopyresized ($newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src _w, $src _h);
- }
- }
- Output (Save) picture
- if (! empty ($save _img))
- {
- $org _funcs [' Save_func '] ($newim, $save _img);
- } else
- {
- Header ($org _funcs [' header ']);
- $org _funcs [' Save_func '] ($newim);
- }
- Imagedestroy ($newim);
- Return Array (' flag ' = = True, ' msg ' = = ');
- }
- /**
- *
- * Watermark Image generation
- * @param $org _img Original image
- * @param $mark _img watermark Tagged Image
- * @param $save _img When its directory does not exist, try to create a directory
- * @param array $option Some basic settings for watermarks include:
- * x: The horizontal position of the watermark, which defaults to the value after subtracting the width of the watermark map
- * Y: The vertical position of the watermark, which defaults to the value after subtracting the height of the watermark map
- * Alpha:alpha value (control transparency), default is 50
- */
- Public Function Water_mark ($org _img, $mark _img, $save _img = ", $option = Array ())
- {
- Check the picture
- $org _ext = $this->is_img ($org _img);
- if (! $org _ext [' flag '])
- {
- return $org _ext;
- }
- $mark _ext = $this->is_img ($mark _img);
- if (! $mark _ext [' flag '])
- {
- return $mark _ext;
- }
- If there is a save path, determine if the path is correct
- if (! empty ($save _img))
- {
- $f = $this->check_dir ($save _img);
- if (! $f [' flag '])
- {
- return $f;
- }
- }
- Get the appropriate canvas
- $org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
- $org _img_im = $org _funcs [' Create_func '] ($org _img);
- $mark _funcs = $this->get_img_funcs ($mark _ext [' msg ']);
- $mark _img_im = $mark _funcs [' Create_func '] ($mark _img);
- copy watermark Picture coordinates
- $mark _img_im_x = 0;
- $mark _img_im_y = 0;
- High-width copy watermark image
- $mark _img_w = imagesx ($mark _img_im);
- $mark _img_h = Imagesy ($mark _img_im);
- $org _img_w = imagesx ($org _img_im);
- $org _img_h = imagesx ($org _img_im);
- Synthesis generating Point coordinates
- $x = $org _img_w-$mark _img_w;
- $org _img_im_x = isset ($option [' x '])? $option [' x ']: $x;
- $org _img_im_x = ($org _img_im_x > $org _img_w or $org _img_im_x < 0)? $x: $org _img_im_x;
- $y = $org _img_h-$mark _img_h;
- $org _img_im_y = isset ($option [' Y '])? $option [' y ']: $y;
- $org _img_im_y = ($org _img_im_y > $org _img_h or $org _img_im_y < 0)? $y: $org _img_im_y;
- Alpha
- $alpha = Isset ($option [' alpha '])? $option [' alpha ']: 50;
- $alpha = ($alpha > or $alpha < 0)? : $alpha;
- Merging pictures
- Imagecopymerge ($org _img_im, $mark _img_im, $org _img_im_x, $org _img_im_y, $mark _img_im_x, $mark _img_im_y, $mark _img_w, $mark _img_h, $alpha);
- Output (Save) picture
- if (! empty ($save _img))
- {
- $org _funcs [' Save_func '] ($org _img_im, $save _img);
- } else
- {
- Header ($org _funcs [' header ']);
- $org _funcs [' Save_func '] ($org _img_im);
- }
- Destroying the canvas
- Imagedestroy ($org _img_im);
- Imagedestroy ($mark _img_im);
- Return Array (' flag ' = = True, ' msg ' = = ');
- }
- /**
- *
- * Check Pictures
- * @param unknown_type $img _path
- * @return Array (' flag ' =>true/false, ' msg ' =>ext/error message)
- */
- Private Function is_img ($img _path)
- {
- if (! file_exists ($img _path))
- {
- Return Array (' flag ' = False, ' msg ' = ' = ' Load picture $img _path failed! " );
- }
- $ext = Explode ('. ', $img _path);
- $ext = Strtolower (end ($ext));
- if (! In_array ($ext, $this->exts))
- {
- Return Array (' flag ' = False, ' msg ' = ' = ' picture $img _path format is incorrect! " );
- }
- Return Array (' flag ' = = True, ' msg ' = $ext);
- }
- /**
- *
- * Returns the correct picture function
- * @param unknown_type $ext
- */
- Private Function Get_img_funcs ($ext)
- {
- Choose
- Switch ($ext)
- {
- Case ' jpg ':
- $header = ' content-type:image/jpeg ';
- $createfunc = ' imagecreatefromjpeg ';
- $savefunc = ' imagejpeg ';
- Break
- Case ' JPEG ':
- $header = ' content-type:image/jpeg ';
- $createfunc = ' imagecreatefromjpeg ';
- $savefunc = ' imagejpeg ';
- Break
- Case ' gif ':
- $header = ' content-type:image/gif ';
- $createfunc = ' imagecreatefromgif ';
- $savefunc = ' imagegif ';
- Break
- Case ' BMP ':
- $header = ' content-type:image/bmp ';
- $createfunc = ' imagecreatefrombmp ';
- $savefunc = ' imagebmp ';
- Break
- Default:
- $header = ' content-type:image/png ';
- $createfunc = ' imagecreatefrompng ';
- $savefunc = ' imagepng ';
- }
- Return Array (' save_func ' = $savefunc, ' create_func ' = $createfunc, ' header ' + $header);
- }
- /**
- *
- * Check and try to create a directory
- * @param $save _img
- */
- Private Function Check_dir ($save _img)
- {
- $dir = dirname ($save _img);
- if (! Is_dir ($dir))
- {
- if (! mkdir ($dir, 0777, True))
- {
- Return Array (' flag ' = False, ' msg ' = ' = ' picture saved directory $dir cannot be created! " );
- }
- }
- Return Array (' flag ' = = True, ' msg ' = = ');
- }
- }
- if (! empty ($_files [' Test '] [' tmp_name ']))
- {
- Example
- $img = new img ();
- Original
- $name = Explode ('. ', $_files [' Test '] [' name ']);
- $org _img = ' d:/test. ' End ($name);
- Move_uploaded_file ($_files [' Test '] [' tmp_name '], $org _img);
- $option = Array (' width ' = $_post [' width '], ' height ' = $_post [' height ']);
- if ($_post [' type '] = = 1)
- {
- $s = $img->resize_image ($org _img, ", $option);
- } else
- {
- $img->thumb_img ($org _img, ", $option);
- }
- Unlink ($org _img);
- }
Copy Code How to use: Watermarking
- $img = new img ();
- $org _img = ' d:/tt.png ';
- $mark _img = ' d:/t.png ';
- Save the watermark picture (if $save_img is empty, the image will be output directly)
- $save _img = ' d:/test99h/testone/sss.png ';
- Watermark Setting Adjustment
- $option = Array (' x ' = +, ' y ' = +, ' alpha ' = 80);
- Create a watermark Picture
- $flag = $img->water_mark ($org _img, $mark _img, $save _img, $option);
Copy Code When we adjust the $option parameters, there is a corresponding change: 1 $option = Array (' x ' = = 0, ' y ' = 0, ' alpha ' = 50); 2$option = Array (' x ' = +, ' y ' = +, ' alpha ' = 80); 3 If you do not set the $option parameter, the default value will be used: If you want a text-only watermark, you can see here: http://www.php.net/manual/zh/image.examples.merged-watermark.php
- Example
- $img = new img ();
- $org _img = ' d:/tt.png ';
- Compress picture (100*100)
- $option = Array (' width ' = +, ' height ' = 100);
- When the $save _img is empty, the image will be output directly to the browser
- $save _img = ' d:/test99h/testone/sss_thumb.png ';
- $flag = $img->thumb_img ($org _img, $save _img, $option);
Copy Code Adjust the size value of the $option:
- $option = Array (' width ' = =, ' height ' = 200);
Copy Code Watermark and compression diagram
- $img = new img ();
- Original
- $org _img = ' d:/tt.png ';
- Watermark Marker Diagram
- $mark _img = ' d:/t.png ';
- Save watermark Picture
- $save _img = ' d:/test99h/testone/sss.png ';
- Watermark Setting Adjustment
- $option = Array (' x ' = +, ' y ' = +, ' alpha ' = 60);
- Create a watermark Picture
- $flag = $img->water_mark ($org _img, $mark _img, $save _img, $option);
- Compress watermark Picture
- $option = Array (' width ' = =, ' height ' = 200);
- Save a compressed diagram
- $save _img2 = ' d:/test99h/testone/sss2_thumb.png ';
- $flag = $img->thumb_img ($save _img, $save _img2, $option); Equal proportional compression Similar
Copy Code When compressing the generated watermark image, the image format generated after compression should be consistent with the original image and watermark image. Otherwise, there are some unknown errors. Another note: Picture compression principle is not my own creation.
|