The following is a simple image processing class, including watermarks and thumbnails. However, there are two ways to generate a thumbnail: one is to compress the image in proportion, and the other is to crop and then compress the image. In my opinion, the difference between compression and cropping is: Compression: ensures a reasonable proportion of the image width and length, and the image integrity. But the actual size is not guaranteed to meet the requirements. Cropping and compression: This ensures a reasonable proportion of the image width and length, and the actual size. However, the image integrity cannot be guaranteed. Image. php
- /**
- *
- * Image processing
- * @ Author FC_LAMP
- * @ Internal: watermarks and thumbnails
- */
- Class Img
- {
- // Image format
- Private $ exts = array ('jpg ', 'jpeg', 'GIF', 'bmp ', 'PNG ');
-
- /**
- *
- *
- * @ Throws Exception
- */
- Public function _ construct ()
- {
- If (! Function_exists ('gd _ info '))
- {
- Throw new Exception ('failed to load the GD library! ');
- }
- }
-
- /**
- *
- * Cropping and compression
- * @ Param $ src_img image
- * @ Param $ save_img: image generated
- * @ Param $ option parameter options, including: $ maxwidth width $ maxheight height
- * Array ('width' => xx, 'height' => xxx)
- * @ Internal
- * Our general method of compressing images is to generate images that are too long or too wide.
- * The image will be squashed. for this case, we should first crop the image and then compress it proportionally.
- */
- Public function thumb_img ($ src_img, $ save_img = '', $ option)
- {
-
- If (empty ($ option ['width']) or empty ($ option ['height'])
- {
- Return array ('flag' => False, 'MSG '=> 'Source Image length and width cannot be less than 0 ');
- }
- $ Org_ext = $ this-> is_img ($ src_img );
- If (! $ Org_ext ['flag'])
- {
- Return $ org_ext;
- }
-
- // If a storage path exists, check whether the path is correct.
- If (! Empty ($ save_img ))
- {
- $ F = $ this-> check_dir ($ save_img );
- If (! $ F ['flag'])
- {
- Return $ f;
- }
- }
-
- // Obtain the corresponding method
- $ Org_funcs = $ this-> get_img_funcs ($ org_ext ['MSG ']);
-
- // Obtain the original size
- $ Source = $ org_funcs ['create _ func'] ($ src_img );
- $ Src_w = imagesx ($ source );
- $ Src_h = imagesy ($ source );
-
- // Adjust the original image (retain the original image shape and crop the image)
- $ Dst_scale = $ option ['height']/$ option ['width']; // aspect ratio of the target image
- $ Src_scale = $ src_h/$ src_w; // aspect ratio of the source image
- If ($ src_scale> = $ dst_scale)
- {// Too high
- $ W = intval ($ src_w );
- $ H = intval ($ dst_scale * $ w );
-
- $ X = 0;
- $ Y = ($ src_h-$ h)/3;
- } Else
- {// Too wide
- $ H = intval ($ src_h );
- $ W = intval ($ h/$ dst_scale );
-
- $ X = ($ src_w-$ w)/2;
- $ Y = 0;
- }
- // Crop
- $ Croped = imagecreatetruecolor ($ w, $ h );
- Imagecopy ($ croped, $ source, 0, 0, $ x, $ y, $ src_w, $ src_h );
- // Zoom
- $ 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) the image
- 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 '=> '');
- }
-
- /**
- *
- * Proportional scaling image
- * @ Param $ src_img original image
- * @ Param $ save_img
- * @ Param $ option parameter setting 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 a storage path exists, check whether the path is correct.
- If (! Empty ($ save_img ))
- {
- $ F = $ this-> check_dir ($ save_img );
- If (! $ F ['flag'])
- {
- Return $ f;
- }
- }
-
- // Obtain the corresponding method
- $ Org_funcs = $ this-> get_img_funcs ($ org_ext ['MSG ']);
-
- // Obtain 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, $ 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) the image
- 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 '=> '');
- }
-
- /**
- *
- * Generate a watermark image
- * @ Param $ org_img original image
- * @ Param $ mark_img watermark marks the image
- * @ Param $ save_img tries to create a directory when its directory does not exist.
- * @ Param array $ option basic settings for watermarks include:
- * X: the horizontal position of the watermark. the default value is the value after the watermark width is subtracted.
- * Y: vertical position of the watermark. the default value is the value after the watermark height is subtracted.
- * Alpha: alpha value (transparency control). The default value is 50.
- */
- Public function water_mark ($ org_img, $ mark_img, $ save_img = '', $ option = array ())
- {
- // Check the image
- $ 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 a storage path exists, check whether the path is correct.
- If (! Empty ($ save_img ))
- {
- $ F = $ this-> check_dir ($ save_img );
- If (! $ F ['flag'])
- {
- Return $ f;
- }
- }
-
- // Obtain the 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 the watermark image coordinates
- $ Mark_img_im_x = 0;
- $ Mark_img_im_y = 0;
- // Copy the watermark image height and width
- $ 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 );
-
- // Combine and generate coordinate points
- $ 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> 100 or $ alpha <0 )? 50: $ alpha;
-
- // Merge images
- Imagecopymerge ($ org_img_im, $ mark_img_im, $ org_img_im_x, $ tags, $ mark_img_im_x, $ mark_img_im_y, $ mark_img_w, $ mark_img_h, $ alpha );
-
- // Output (save) the image
- 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 );
- }
- // Destroy the canvas
- Imagedestroy ($ org_img_im );
- Imagedestroy ($ mark_img_im );
- Return array ('flag' => True, 'MSG '=> '');
-
- }
-
- /**
- *
- * Check images
- * @ 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 '=> "loading image $ img_path failed! ");
- }
- $ Ext = explode ('.', $ img_path );
- $ Ext = strtolower (end ($ ext ));
- If (! In_array ($ ext, $ this-> exts ))
- {
- Return array ('flag' => False, 'MSG '=> "The image $ img_path format is incorrect! ");
- }
- Return array ('flag' => True, 'MSG '=> $ ext );
- }
-
- /**
- *
- * Returns the correct image function.
- * @ Param unknown_type $ ext
- */
- Private function get_img_funcs ($ ext)
- {
- // Select
- 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 '=> "The image storage directory $ dir cannot be created! ");
- }
- }
- Return array ('flag' => True, 'MSG '=> '');
- }
- }
- If (! Empty ($ _ FILES ['test'] ['tmp _ name'])
- {
- // Example
- $ Img = new Img ();
- // Source image
- $ 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 );
- }
Usage: Watermark
- $ Img = new Img ();
- $ Org_img = 'd:/tt.png ';
- $ Mark_img = 'd:/t.png ';
- // Save the watermark image (if $ save_img is empty, the image will be output directly)
- $ Save_img = 'd:/test99h/testone/sss.png ';
- // Adjust watermark settings
- $ Option = array ('x' => 50, 'y' => 50, 'alpha' => 80 );
- // Generate a watermark image
- $ Flag = $ img-> water_mark ($ org_img, $ mark_img, $ save_img, $ option );
When we adjust the $ option parameter, the following changes occur: 1 $ option = array ('x' => 0, 'y' => 0, 'alpha' => 50 ); 2 $ option = array ('x' => 50, 'y' => 50, 'alpha' => 80 ); 3. if you do not set the $ option parameter, the default value is used: For plain text watermark, see here: http://www.php.net/manual/zh/image.examples.merged-watermark.php
- // Example
- $ Img = new Img ();
- $ Org_img = 'd:/tt.png ';
- // Compress the image (100*100)
- $ Option = array ('width' => 100, 'height' => 100 );
- // When $ save_img is empty, the image is directly output to the browser.
- $ Save_img = 'd:/test99h/testone/sss_thumb.png ';
- $ Flag = $ img-> thumb_img ($ org_img, $ save_img, $ option );
Adjust the value of $ option:
- $ Option = array ('width' => 200, 'height' => 200 );
Watermarks and compression graphs
- $ Img = new Img ();
- // Source image
- $ Org_img = 'd:/tt.png ';
- // Watermark markup image
- $ Mark_img = 'd:/t.png ';
- // Save the watermark image
- $ Save_img = 'd:/test99h/testone/sss.png ';
- // Adjust watermark settings
- $ Option = array ('x' => 50, 'y' => 50, 'alpha' => 60 );
- // Generate a watermark image
- $ Flag = $ img-> water_mark ($ org_img, $ mark_img, $ save_img, $ option );
- // Compress the watermark image
- $ Option = array ('width' => 200, 'height' => 200 );
- // Save the compression graph
- $ Save_img2 = 'd:/test99h/testone/sss2_thumb.png ';
- $ Flag = $ img-> thumb_img ($ save_img, $ save_img2, $ option); // proportional compression is similar
When compressing the generated watermark image, the format of the compressed image should be consistent with that of the original image and the watermark image. Otherwise, some unknown errors may occur. Note: the image compression principle is not created by myself.
|