Below is a simple picture processing class whose functions include: watermark, thumbnail, etc.
However, there are two ways to generate thumbnails: one is to directly compress the image proportionally, and the other is to crop and then compress the image. In my opinion, the difference between equal-case compression and cropping compression is:
Equal example compression: It can ensure that the width and length ratio of the image is reasonable and the image is complete. However, actual size is not guaranteed to meet requirements.
Cropping and compression: It can ensure that the width and length ratio of the picture is reasonable, and the actual size can also be guaranteed. However, the integrity of the picture cannot be guaranteed.image.php
- /**
- *
- * Image processing class
- * @author FC_LAMP
- * @internal functions include: watermark, thumbnail
- */
- 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 GD library!' );
- }
- }
-
- /**
- *
- * Cropping and compression
- * @param $src_img image
- * @param $save_img generated image
- * @param $option parameter options, including: $maxwidth width $maxheight height
- * array('width'=> xx,'height'=>xxx)
- * @internal
- * Our general image compression method, when the image is too long or too wide, the generated image
- * will be "squashed". For this, crop first and then 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' => 'The length and width of the original image 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 whether the path is correct
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if (! $f ['flag'])
- {
- return $f;
- }
- }
-
- // Get the corresponding 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 original shape of the image and crop the image)
- $dst_scale = $option ['height'] / $option ['width ']; //Target image aspect ratio
- $src_scale = $src_h / $src_w; // Original image 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
- { // Too wide
- $h = intval ( $src_h );
- $w = intval ( $h / $dst_scale );
-
- $x = ($src_w - $w) / 2;
- $y = 0;
- }
- // Cropped
- $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) 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' => '' );
- }
-
- /**
- *
- * Scale image
- * @param $src_img Original image
- * @param $save_img Where to save
- * @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 there is a save path, determine whether the path is correct
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $save_img );
- if ( ! $f ['flag'])
- {
- return $f;
- }
- }
-
- //Get the corresponding 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' => '' );
- }
-
- /**
- *
- * Generate watermark image
- * @param $org_img Original image
- * @param $mark_img Watermark image
- * @param $save_img When its directory does not exist, it will try to create the directory
- * @param array $option is Some basic settings of the watermark include:
- * x: the horizontal position of the watermark, the default is the value after subtracting the width of the watermark image
- * y: the vertical position of the watermark, the default is the value after subtracting the height of the watermark image
- * 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 whether the path is correct
- if (! empty ( $save_img ))
- {
- $f = $this->check_dir ( $ save_img );
- if (! $f ['flag'])
- {
- return $f;
- }
- }
-
- //Get the corresponding 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 image coordinates
- $mark_img_im_x = 0;
- $mark_img_im_y = 0;
- //Copy 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 );
-
- //Synthetic generated 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 > 100 or $alpha < 0)? 50: $ Alpha;
-
- // Merge picture
- Imagecopymerge ($ ORG_IMG_IM, $ Mark_img_im, $ ORG_IMG_I_X, $ ORG_IMG_IM_Y, $ Mark_img_X, $ Mark_img_im_ y, $ mark_img_w, $mark_img_h, $ alpha);
-
- // output (Save) 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 the image
- * @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' => "Failed to load image $img_path! " );
- }
- $ext = explode ( '.', $img_path );
- $ext = strtolower ( end ( $ext ) );
- if (! in_array ( $ext, $this->exts ))
- {
- return array ('flag' => False, 'msg' => "The format of the image $img_path is incorrect!" );
- }
- return array ('flag' => True, 'msg' => $ext );
- }
-
- /**
- *
- * Return the correct image function
- * @param unknown_type $ext
- */
- private function get_img_funcs($ext)
- {
- //选择
- 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 the 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' => "图片保存目录 $dir 无法创建!" );
- }
- }
- return array ('flag' => True, 'msg' => '' );
- }
- }
-
- if (! empty ( $_FILES ['test'] ['tmp_name'] ))
- {
- //例子
- $img = new Img ();
- //原图
- $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 );
- }
-
-
复制代码
使用方式:
水印
- $img = new Img ();
- $org_img = 'D:/tt.png';
- $mark_img = 'D:/t.png';
- //保存水印图片(如果$save_img为空时,将会直接输出图片)
- $save_img = 'D:/test99h/testone/sss.png';
- //水印设置调节
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 80 );
- //生成水印图片
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
-
-
复制代码
当我们调节 $option 参数时,会有相应变化:
1 $option = array ('x' => 0, 'y' => 0, 'alpha' => 50 );
2$option = array ('x' => 50, 'y' => 50, 'alpha' => 80 );
3 如果你不设置$option 参数,将会采用默认值:
如果要纯文字式的水印,可以参看这里:http://www.php.net/manual/zh/image.examples.merged-watermark.php
- //例子
- $img = new Img ();
- $org_img = 'D:/tt.png';
- //压缩图片(100*100)
- $option = array ('width' => 100, 'height' => 100 );
-
- //$save_img为空时,将会直接输出图像到浏览器
-
- $save_img = 'D:/test99h/testone/sss_thumb.png';
- $flag = $img->thumb_img ( $org_img, $save_img, $option );
-
-
复制代码
调节$option的大小值:
- $option = array ('width' => 200, 'height' => 200);
-
-
复制代码
水印与压缩图
- $img = new Img ();
- //原图
- $org_img = 'D:/tt.png';
- //水印标记图
- $mark_img = 'D:/t.png';
- //保存水印图片
- $save_img = 'D:/test99h/testone/sss.png';
- //水印设置调节
- $option = array ('x' => 50, 'y' => 50, 'alpha' => 60 );
- //生成水印图片
- $flag = $img->water_mark ( $org_img, $mark_img, $save_img, $option );
- //压缩水印图片
- $option = array ('width' => 200, 'height' => 200 );
- //保存压缩图
- $save_img2 = 'D:/test99h/testone/sss2_thumb.png';
- $flag = $img->thumb_img ( $save_img, $save_img2, $option ); //等比例压缩类似
复制代码
When compressing the generated watermark image, the format of the image generated after compression should be consistent with the original image and watermark image. Otherwise, some unknown errors will occur.
Also note: The image compression principle was not invented by me.
|