-
-
/** Thumbnail generation class, supports imagemagick and gd library processing - * Date: 2013-07-15
- * Author: fdipzone
- * Ver: 1.2
- * edit: bbs.it-home.org
- * Func:
- * public set_config: Set parameters
- * public create_thumb: Generate thumbnail image
- * private fit: Thumbnail image
- * private crop: Crop image
- * private gd_fit: GD library thumbnail image
- * private gd_crop: GD library crop image
- * private get_size: get the size to be converted
- * private get_crop_offset: get the offset of the crop
- * private add_watermark: add watermark
- * private check_handler: judgment processing Whether the program has been installed
- * private create_dirs: Create a directory
- * private exists: Determine whether the parameter exists
- * private to_log: Record the log
- * private hex2rgb: Convert hex color to rgb color
- * private get_file_ext: Get the image type
- *
- * ver : 1.1 Added GD library processing
- * ver: 1.2 Added width and height error parameter processing
- * Added conversion to RGB processing when the image colorspace is not RGB
- * Fixed the problem of transparent invalid area when using crop to save as gif, use +repage parameters, just delete the transparent invalid area
- *
- * tips: It is recommended to use imagemagick
- * The GD library does not support transparency watermarks. If you must use transparent watermarks, please make the watermark image transparent.
- * If the GD library outputs a gif with a transparent watermark, there will be problems.
- */
-
- class PicThumb{ // class start
-
- private $_log = null; // log file
- private $_handler = null; // Program for image processing, imagemagick/gd library
- private $_type = 'fit '; // fit or crop
- private $_source = null; // Original image path
- private $_dest = null; // Thumbnail path
- private $_watermark = null; // Watermark image
- private $_opacity = 75; / / Watermark image transparency, gd library does not support
- private $_gravity = 'SouthEast'; // Watermark placement location NorthWest, North, NorthEast, West, Center, East, SouthWest, South, SouthEast
- private $_geometry = '+10+ 10'; // Watermark positioning, gd library does not support
- private $_croppos = 'TL'; // Screenshot location TL TM TR ML MM MR BL BM BR
- private $_bgcolor = null; // Filled background color
- private $_quality = 90; // Generated image quality
- private $_width = null; // Specify area width
- private $_height = null; // Specify area height
-
-
- // Initialization
- public function __construct($logfile=' '){
- if($logfile!=''){
- $this->_log = $logfile;
- }
- }
-
-
- // Set parameters
- public function set_config($param=array()){
- $this->_handler = $this->exists($param, 'handler')? strtolower($param['handler']) : null;
- $this->_type = $this->exists( $param, 'type')? strtolower($param['type']) : 'fit';
- $this->_watermark = $this->exists($param, 'watermark')? $param[' watermark'] : null;
- $this->_opacity = $this->exists($param, 'opacity')? $param['opacity'] : 75;
- $this->_gravity = $this- >exists($param, 'gravity')? $param['gravity'] : 'SouthEast';
- $this->_geometry = $this->exists($param, 'geometry')? $param[ 'geometry'] : '+10+10';
- $this->_croppos = $this->exists($param, 'croppos')? $param['croppos'] : 'TL';
- $this ->_bgcolor = $this->exists($param, 'bgcolor')? $param['bgcolor'] : null;
- $this->_quality = $this->exists($param, 'quality ')? $param['quality'] : 90;
- $this->_width = $this->exists($param, 'width')? $param['width'] : null;
- $this- >_height = $this->exists($param, 'height')? $param['height'] : null;
- }
-
- /**Create thumbnail image
- * @param String $source original image
- * @param String $dest target image
- * @return boolean
- */
- public function create_thumb($source, $ dest){
-
- // Check whether the handler used is installed
- if(!$this->check_handler()){
- $this->to_log('handler not installed');
- return false;
- }
-
- // Determine whether the width and height of the area are correct
- if(!is_numeric($this->_width) || !is_numeric($this->_height) || $this->_width<=0 || $this- >_height<=0){
- $this->to_log('width or height invalid');
- return false;
- }
-
- // Determine whether the source file exists
- if(!file_exists($source)){
- $this->to_log($source.' not exists');
- return false;
- }
-
- // Create target file path
- if(!$this->create_dirs($dest)){
- $this ->to_log(dirname($dest).' create fail');
- return false;
- }
-
- $this->_source = $source; // Source file
- $this->_dest = $dest; // Target file
-
- // Process the image
- switch($this->_type){
- case 'fit':
- if($this->_handler=='imagemagick'){
- return $this-> fit();
- }else{
- return $this->gd_fit();
- }
- break;
-
- case 'crop':
- if($this->_handler=='imagemagick'){
- return $ this->crop();
- }else{
- return $this->gd_crop();
- }
- break;
-
- default:
- $this->to_log($this->_type.' not fit and crop');
- return false;
- }
-
- }
-
-
- /**Compress or stretch the image proportionally
- * @return boolean
- */
- private function fit(){
-
- // Determine whether to fill the background
- $bgcolor = ($this->_bgcolor!=null)?
- sprintf(" -background '% s' -gravity center -extent '%sx%s' ", $this->_bgcolor, $this->_width, $this->_height) : "";
-
- // Determine whether to convert to RGB
- $source_info = getimagesize($this->_source);
- $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
-
- // Command line
- $cmd = sprintf("convert -resize '%sx%s' '%s' %s -quality %s %s '%s'", $this->_width, $this->_height, $this->_source, $bgcolor, $this->_quality, $colorspace, $this->_dest);
-
- // Record the executed command
- $this->to_log ($cmd);
-
- // Execute command
- exec($cmd);
-
- // Add watermark
- $this->add_watermark($this->_dest);
-
- return is_file($this-> _dest)? true : false;
-
- }
-
-
- /**Crop image
- * @return boolean
- */
- private function crop(){
-
- // Get the generated image size
- list($pic_w, $pic_h) = $this- >get_size();
-
- // Get the offset of the screenshot
- list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
-
- // Determine whether to convert to RGB
- $source_info = getimagesize($this->_source);
- $colorspace = (!isset($source_info['channels']) || $source_info['channels']!=3)? ' -colorspace RGB ' : '';
-
- // Command line
- $cmd = sprintf("convert -resize '%sx%s' '%s' -quality %s %s -crop %sx%s+%s+%s +repage '%s '", $pic_w, $pic_h, $this->_source, $this->_quality, $colorspace, $this->_width, $this->_height, $offset_w, $offset_h, $this-> ;_dest);
-
- // Record the executed command
- $this->to_log($cmd);
-
- // Execute the command
- exec($cmd);
-
- // Add watermark
- $this->add_watermark ($this->_dest);
-
- return is_file($this->_dest)? true : false;
-
- }
-
-
- /**GD library compresses or stretches images proportionally
- * @return boolean
- */
- private function gd_fit(){
-
- / / Get the generated image size
- list($pic_w, $pic_h) = $this->get_size();
-
- list($owidth, $oheight, $otype) = getimagesize($this->_source);
-
- switch($otype){
- case 1: $source_img = imagecreatefromgif($this->_source); break;
- case 2: $source_img = imagecreatefromjpeg($this->_source); break;
- case 3: $ source_img = imagecreatefrompng($this->_source); break;
- default: return false;
- }
-
- // Shrink/stretch the image proportionally
- $new_img = imagecreatetruecolor($pic_w, $pic_h);
- imagecopyresampled( $new_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);
-
- // Determine whether to fill the background
- if($this->_bgcolor!=null){
- $bg_img = imagecreatetruecolor($this->_width, $this->_height);
- $rgb = $this->hex2rgb($this->_bgcolor);
- $bgcolor =imagecolorallocate($bg_img, $rgb ['r'], $rgb['g'], $rgb['b']);
- imagefill($bg_img, 0, 0, $bgcolor);
- imagecopy($bg_img, $new_img, (int)( ($this->_width-$pic_w)/2), (int)(($this->_height-$pic_h)/2), 0, 0, $pic_w, $pic_h);
- $new_img = $ bg_img;
- }
-
- // Get the type of the target image
- $dest_ext = $this->get_file_ext($this->_dest);
-
- // Generate the image
- switch($dest_ext){
- case 1: imagegif ($new_img, $this->_dest, $this->_quality); break;
- case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
- case 3 : imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
- }
-
- if(isset($source_img)){
- imagedestroy( $source_img);
- }
-
- if(isset($new_img)){
- imagedestroy($new_img);
- }
-
- // Add watermark
- $this->add_watermark($this->_dest);
-
- return is_file($this->_dest)? true : false;
-
- }
-
-
- /**GD library crop image
- * @return boolean
- */
- private function gd_crop(){
-
- // Get the generated image size
- list($pic_w, $pic_h) = $this->get_size();
-
- // Get the offset of the screenshot
- list($offset_w, $offset_h) = $this->get_crop_offset($pic_w, $pic_h);
-
- list($owidth, $oheight, $otype) = getimagesize($this-> ;_source);
-
- switch($otype){
- case 1: $source_img = imagecreatefromgif($this->_source); break;
- case 2: $source_img = imagecreatefromjpeg($this->_source); break;
- case 3: $source_img = imagecreatefrompng($this->_source); break;
- default: return false;
- }
-
- // Shrink/stretch the image proportionally
- $tmp_img = imagecreatetruecolor($pic_w, $pic_h );
- imagecopyresampled($tmp_img, $source_img, 0, 0, 0, 0, $pic_w, $pic_h, $owidth, $oheight);
-
- // Crop the picture
- $new_img = imagecreatetruecolor($this->_width , $this->_height);
- imagecopyresampled($new_img, $tmp_img, 0, 0, $offset_w, $offset_h, $this->_width, $this->_height, $this->_width, $ this->_height);
-
- // Get the type of the target image
- $dest_ext = $this->get_file_ext($this->_dest);
-
- // Generate the image
- switch($dest_ext){
- case 1: imagegif($new_img, $this->_dest, $this->_quality); break;
- case 2: imagejpeg($new_img, $this->_dest, $this->_quality); break;
- case 3: imagepng($new_img, $this->_dest, (int)(($this->_quality-1)/10)); break;
- }
-
- if(isset($source_img)){
- imagedestroy($source_img);
- }
-
- if(isset($tmp_img)){
- imagedestroy($tmp_img);
- }
-
- if(isset($new_img)){
- imagedestroy($new_img);
- }
-
- // Add watermark
- $this->add_watermark($this->_dest);
-
- return is_file($this->_dest)? true : false;
-
- }
-
-
- /**Get the size generated by the target image
- * @return Array $width, $height
- */
- private function get_size(){
- list($owidth, $oheight) = getimagesize($this->_source);
- $width = (int)($this->_width);
- $height = ( int)($this->_height);
-
- switch($this->_type){
- case 'fit':
- $pic_w = $width;
- $pic_h = (int)($pic_w*$oheight/ $owidth);
- if($pic_h>$height){
- $pic_h = $height;
- $pic_w = (int)($pic_h*$owidth/$oheight);
- }
- break;
- case 'crop':
- if($owidth>$oheight){
- $pic_h = $height;
- $pic_w = (int)($pic_h*$owidth/$oheight);
- }else{
- $pic_w = $width;
- $pic_h = (int)($pic_w*$oheight/$owidth);
- }
- break;
- }
-
- return array($pic_w, $pic_h);
- }
-
-
- /**Get the offset of the screenshot
- * @param int $pic_w Picture width
- * @param int $pic_h Picture height
- * @return Array $offset_w, $offset_h
- */
- private function get_crop_offset($pic_w, $pic_h){
- $offset_w = 0;
- $offset_h = 0;
-
- switch(strtoupper($this->_croppos)){
- case 'TL':
- $offset_w = 0;
- $offset_h = 0;
- break;
-
- case 'TM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = 0;
- break;
-
- case 'TR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = 0;
- break;
-
- case 'ML':
- $offset_w = 0;
- $offset_h = (int)(($pic_h-$this->_height)/2);
- break;
-
- case 'MM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = (int)(($pic_h-$this->_height)/2);
- break;
-
- case 'MR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = (int)(($pic_h-$this->_height)/2);
- break;
-
- case 'BL':
- $offset_w = 0;
- $offset_h = (int)($pic_h-$this->_height);
- break;
-
- case 'BM':
- $offset_w = (int)(($pic_w-$this->_width)/2);
- $offset_h = (int)($pic_h-$this->_height);
- break;
-
- case 'BR':
- $offset_w = (int)($pic_w-$this->_width);
- $offset_h = (int)($pic_h-$this->_height);
- break;
- }
-
- return array($offset_w, $offset_h);
- }
-
-
- /**Add watermark
- * @param String $dest image path
- */
- private function add_watermark($dest){
- if($this->_watermark!=null && file_exists($this->_watermark) && file_exists($dest)){
- list($owidth, $oheight, $otype) = getimagesize($dest);
- list($w, $h, $wtype) = getimagesize($this->_watermark);
-
- // 水印图比原图要小才加水印
- if($w<=$owidth && $h<=$oheight){
-
- if($this->_handler=='imagemagick'){ // imagemagick 添加水印
-
- $cmd = sprintf("composite -gravity %s -geometry %s -dissolve %s '%s' %s %s", $this->_gravity, $this->_geometry, $this->_opacity, $this->_watermark, $dest, $dest);
-
- $this->to_log($cmd);
-
- exec($cmd);
-
- }else{ // gd 添加水印
-
- switch($wtype){
- case 1: $water_img = imagecreatefromgif($this->_watermark); break;
- case 2: $water_img = imagecreatefromjpeg($this->_watermark); break;
- case 3: $water_img = imagecreatefrompng($this->_watermark); break;
- default: return false;
- }
-
- switch($otype){
- case 1: $dest_img = imagecreatefromgif($dest); break;
- case 2: $dest_img = imagecreatefromjpeg($dest); break;
- case 3: $dest_img = imagecreatefrompng($dest); break;
- default: return false;
- }
-
- // 水印位置
- switch(strtolower($this->_gravity)){
- case 'northwest':
- $posX = 0;
- $posY = 0;
- break;
- case 'north':
- $posX = ($owidth - $w) / 2;
- $posY = 0;
- break;
- case 'northeast':
- $posX = $owidth - $w;
- $posY = 0;
- break;
- case 'west':
- $posX = 0;
- $posY = ($oheight - $h) / 2;
- break;
- case 'center':
- $posX = ($owidth - $w) / 2;
- $posY = ($oheight - $h) / 2;
- break;
- case 'east':
- $posX = $owidth - $w;
- $posY = ($oheight - $h) / 2;
- break;
- case 'southwest':
- $posX = 0;
- $posY = $oheight - $h;
- break;
- case 'south':
- $posX = ($owidth - $w) / 2;
- $posY = $oheight - $h;
- break;
- case 'southeast':
- $posX = $owidth - $w;
- $posY = $oheight - $h;
- break;
- }
-
- imagealphablending($dest_img, true);
- imagecopy($dest_img, $water_img, $posX, $posY, 0, 0, $w, $h);
-
- switch($otype){
- case 1:imagegif($dest_img, $dest, $this->_quality); break;
- case 2:imagejpeg($dest_img, $dest, $this->_quality); break;
- case 3:imagepng($dest_img, $dest, (int)(($this->_quality-1)/10)); break;
- }
-
- if(isset($water_img)){
- imagedestroy($water_img);
- }
-
- if(isset($dest_img)){
- imagedestroy($dest_img);
- }
-
- }
- }
- }
- }
-
-
- /**Determine whether the handler is installed
- * @return boolean
- */
- private function check_handler(){
-
- $handler = $this->_handler;
-
- if(!in_array($handler, array('imagemagick', 'gd', null))){
- return false;
- }
-
- // 检查是否有安装imagemagick
- $imagemagick_installed = strstr(shell_exec('convert -version'),'Version: ImageMagick')!=''? true : false;
-
- // 检查是否有安装gd库
- $gd_installed = function_exists('gd_info')? true : false;
-
- switch($handler){
- case 'imagemagick':
- return $imagemagick_installed;
- break;
-
- case 'gd':
- return $gd_installed;
- break;
-
- case null:
- if($imagemagick_installed){
- $this->_handler = 'imagemagick';
- return true;
- }
-
- if($gd_installed){
- $this->_handler = 'gd';
- return true;
- }
- break;
- }
-
- return false;
- }
-
-
- /**Create image directory
- * @param String $path
- * @return boolean
- */
- private function create_dirs($dest){
- if(!is_dir(dirname($dest))){
- return mkdir(dirname($dest), 0777, true);
- }
- return true;
- }
-
-
- /**Determine whether the parameter exists
- * @param Array $obj array object
- * @param String $key the key to be found
- * @return boolean
- */
- private function exists($obj,$key=''){
- if($key==''){
- return isset($obj) && !empty($obj);
- }else{
- $keys = explode('.',$key);
- for($i=0,$max=count($keys); $i<$max; $i++){
- if(isset($obj[$keys[$i]])){
- $obj = $obj[$keys[$i]];
- }else{
- return false;
- }
- }
- return isset($obj) && !empty($obj);
- }
- }
-
-
- /**Record log
- * @param String $msg The log message to be recorded
- */
- private function to_log($msg){
- if($this->_log){
- $msg = '['.date('Y-m-d H:i:s').']'.$msg."rn";
- file_put_contents($this->_log, $msg, FILE_APPEND);
- }
- }
-
-
- /**hex color to rgb color
- * @param String $color hex color
- * @return Array
- */
- private function hex2rgb($hexcolor){
- $color = str_replace('#', '', $hexcolor);
- if (strlen($color) > 3) {
- $rgb = array(
- 'r' => hexdec(substr($color, 0, 2)),
- 'g' => hexdec(substr($color, 2, 2)),
- 'b' => hexdec(substr($color, 4, 2))
- );
- } else {
- $r = substr($color, 0, 1) . substr($color, 0, 1);
- $g = substr($color, 1, 1) . substr($color, 1, 1);
- $b = substr($color, 2, 1) . substr($color, 2, 1);
- $rgb = array(
- 'r' => hexdec($r),
- 'g' => hexdec($g),
- 'b' => hexdec($b)
- );
- }
- return $rgb;
- }
-
-
- /**Get the image type
- * @param String $file image path
- * @return int
- */
- private function get_file_ext($file){
- $filename = basename($file);
- list($name, $ext)= explode('.', $filename);
-
- $ext_type = 0;
-
- switch(strtolower($ext)){
- case 'jpg':
- case 'jpeg':
- $ext_type = 2;
- break;
- case 'gif':
- $ext_type = 1;
- break;
- case 'png':
- $ext_type = 3;
- break;
- }
-
- return $ext_type;
- }
-
- } // class end
-
- ?>
- demo:
- [php] view plaincopy
- define('ROOT', dirname(__FILE__));
-
- require(ROOT."/PicThumb.class.php");
-
- $logfile = ROOT.'/PicThumb.log';
- $source1 = ROOT.'/pic/source.jpg';
- $dest1 = ROOT.'/pic/1.jpg';
- $dest2 = ROOT.'/pic/2.gif';
- $dest3 = ROOT.'/pic/3.png';
-
- $source2 = ROOT.'/pic/source_cmyk.jpg';
- $dest4 = ROOT.'/pic/4.jpg';
- $dest5 = ROOT.'/pic/5.gif';
- $dest6 = ROOT.'/pic/6.png';
-
- $watermark = ROOT.'/pic/watermark.png';
-
- // 按比例生成缩略图
- $param = array(
- 'type' => 'fit',
- 'width' => 100,
- 'height' => 100,
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source1, $dest1);
-
- if($flag){ // bbs.it-home.org
- echo '';
- }else{
- echo 'create thumb fail';
- }
-
- // 按比例生成缩略图,不足部分用#FF0000填充
- $param = array(
- 'type' => 'fit',
- 'width' => 100,
- 'height' => 100,
- 'bgcolor' => '#FFFF00'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source1, $dest2);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
-
- // 裁剪250x250的缩略图,裁剪位置是底部中间,水印位置西南,透明度50
- $param = array(
- 'type' => 'crop',
- 'croppos' => 'BM',
- 'width' => 250,
- 'height' => 250,
- 'watermark' => $watermark,
- 'opacity' => 50,
- 'gravity' => 'SouthWest'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source1, $dest3);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
-
- // 按比例生成缩略图 CMYK格式
- $param = array(
- 'type' => 'fit',
- 'width' => 100,
- 'height' => 100,
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source2, $dest4);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
// Generate thumbnails proportionally, fill the missing parts with #FF0000 in CMYK format
- $param = array(
- 'type' => 'fit',
- 'width' => 100,
- 'height' => 100,
- 'bgcolor' => '#FFFF00'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb($source2, $dest5);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
-
- // Crop a 250x250 thumbnail, the cropping position is the bottom middle, the watermark position is southwest, and the transparency is 50 CMYK format
- $param = array(
- 'type' => ; 'crop',
- 'croppos' => 'BM',
- 'width' => 250,
- 'height' => 250,
- 'watermark' => $watermark,
- 'opacity' => ; 50,
- 'gravity' => 'SouthWest'
- );
-
- $obj = new PicThumb($logfile);
- $obj->set_config($param);
- $flag = $obj->create_thumb ($source2, $dest6);
-
- if($flag){
- echo '';
- }else{
- echo 'create thumb fail';
- }
- ?>
-
Copy code
>>> PHP thumbnail generation class source code download address
|