- /**
- * Source: Internet
- */
- class image
- {
- var $w_pct = 100; //Transparency
- var $w_quality = 100; //Quality
- var $w_minwidth = 500; / /Minimum width
- var $w_minheight = 500; //Minimum height
- var $interlace = 0; //Whether the image is interlaced
- var $fontfile; //Font file
- var $w_img; //Default watermark image
-
- function __construct()
- {
- $this->fontfile = $_SERVER['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';
- $this->w_img = '';
- }
- function image( )
- {
- $this->__construct();
- }
-
- function info($img)
- {
- $imageinfo = getimagesize($img); //Return image information array 0=>Width pixels 1= >High pixel 2=> is the image type mark 3 => is a text string, the content is "height="yyy" width="xxx"",
- if($imageinfo === false) return false ;
- $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //Get the image file type $imageinfo[2] is the mark of the image type
- $imagesize = filesize($img); // Image size
- $info = array(
- 'width'=>$imageinfo[0],
- 'height'=>$imageinfo[1],
- 'type'=>$imagetype,
- 'size'=> ;$imagesize,
- 'mime'=>$imageinfo['mime']
- );
- return $info;
- }
-
- /**
- * 缩略图
- *
- * @param string $image
- * @param string $filename
- * @param int $maxwidth
- * @param int $maxheight
- * @param string $suffix
- * @param int $autocut
- * @return string
- */
- function thumb($image, $filename = '' ,$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)
- {
- if( !$this->check($image)) return false;
- $info = $this- >info($image); //Get image information
- if($info === false) return false;
- $srcwidth = $info['width']; //Source image width
- $srcheight = $info[ 'height']; //The source image is high
- $pathinfo = pathinfo($image);
- $type = $pathinfo['extension']; //Get the extension
- if(!$type) $type = $info[ 'type']; //If not obtained, use $info['type']
- $type = strtolower($type);
- unset($info);
- $scale = min($maxwidth/$srcwidth, $ maxheight/$srcheight); //Get the thumbnail ratio
- //Get the ratio according to the source image
- $createwidth = $width = (int) ($srcwidth*$scale); //Get the thumbnail width();
- $createheight = $height = (int)($srcheight*$scale);//(); //Get the thumbnail height
- $psrc_x = $psrc_y = 0;
- if($autocut) //According to the proportion of the thumbnail To get
- {
- if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //If the thumbnail is proportionally narrower than the source image
- {
- $width = $maxheight/$height *$width; //The width is processed according to the corresponding proportion
- $height = $maxheight;// //The height remains unchanged
- }
- elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) //If the thumbnail is proportionally wider than the source image
- {
- $height = $maxwidth/$width*$height;//
- $width = $maxwidth;
- }
- if($maxwidth == '55') {
- $width = '55';
- $createwidth = '55';
- }
- if($maxwidth == '110'){
- $width = '110';
- $createwidth = '110';
- }
-
- }
- if($srcwidth<='280' && $maxwidth != '110'){
- $createwidth= $srcwidth;
- $createheight = $srcheight;
- }
- $createfun = 'imagecreatefrom'.($type= ='jpg' ? 'jpeg' : $type); //Find different image processing functions
- $srcimg = $createfun($image); //Return the image identifier
- if($type != 'gif' && function_exists ('imagecreatetruecolor')){
- $thumbimg = imagecreatetruecolor($createwidth, $createheight); //Create a new true color image
- }else{
- if($maxwidth == '110' && $srcheight >= $srcwidth){
- $height1 = 72;
- $thumbimg = imagecreate($width, $height1); //Create a new palette-based Image
- }elseif ($maxwidth == '110' && $srcheight <= $srcwidth){
- $width1 = 110;
- $thumbimg = imagecreate($width1, $height); //Create a new palette-based Image
- }else{
- $thumbimg = imagecreate($width, $height); //Create a new palette-based image
- }
- }
- if(function_exists('imagecopyresampled')){ //Resample and copy part of the image And adjust the size, true color
- //imagecopyresampled(new image, source image, x distance from the upper left corner of the new image, y distance from the upper left corner of the new image, x distance from the upper left corner of the source image, y distance from the upper left corner of the source image, width of the new image, new Image height, source image width, source image height)
- if($maxwidth == '110' && $srcheight >= $srcwidth){
- $psrc_x = 0;
- $psrc_yz = $height/2;
- $psrc_y = $psrc_yz;
- $width = '110';
- }
- if($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth <= '110'){
- $psrc_x = 0;
- $psrc_yz = $height/2;
- $psrc_y = $psrc_yz;
- $width = '110';
- }
- if($maxheight == '72' && $srcheight <= $srcwidth && $srcheight<= '72' && $srcwidth >= '110'){
- $cha = ($maxheight-$srcheight)/2;
- $psrc_y = -($cha);
- $width = $srcwidth;
- $height = '72';
- $srcheight = '72';
- }
- imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
-
- }else{ //Copy part of the image and resize, palette
- imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
- }
- if($type=='gif' || $type=='png')
- {
- //imagecolorallocate assigns a color to an image
- $background_color = imagecolorallocate($thumbimg, 0, 255, 0); / / Fill the background color of the image based on the palette, assign a green
- // imagecolortransparent Define a color as a transparent color
- imagecolortransparent($thumbimg, $background_color); // Set to a transparent color, if this line is commented out Output a green image
- }
- // imageinterlace activates or disables interlacing
- if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
- $ imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
- //imagejpeg imagegif imagepng
- if(empty($filename)) $filename = substr($image, 0, strrpos( $image, '.')).$suffix.'.'.$type; //Get the file name
- //aaa.gif aaa_thumb.gif
- $imagefun($thumbimg, $filename); //New image
- imagedestroy ($thumbimg); //Destroy the thumbnail
- imagedestroy($srcimg); //Destroy the source image
- return $filename;
- }
- /**
- * Limit width or height
- *
- * @param string $image
- * @param int $maxwidth
- * @param int $maxheight
- * @param string $suffix
- * @return string
- */
- function thumb3($image,$maxwidth = 0, $maxheight = 0, $suffix='_thumb')
- {
- if( !$this->check($image)) return false;
- $info = $this->info($image); //Get Image information
- if($info === false) return false;
- $srcwidth = $info['width']; //Source image width
- $srcheight = $info['height']; //Source image height
- $pathinfo = pathinfo($image);
- $type = $pathinfo['extension']; //Get the extension
- if(!$type) $type = $info['type']; //If not obtained , use $info['type']
- $type = strtolower($type);
- unset($info);
- if($maxwidth==0){
- $scale = $maxheight/$srcheight;
- if($ scale<1){
- $createwidth = $srcwidth*$scale;
- $createheight = $maxheight;
- }else{
- $createwidth = $srcwidth;
- $createheight = $srcheight;
- }
- }
- if($maxheight==0){
- $scale = $maxwidth/$srcwidth;
- if($scale<1){
- $createwidth = $maxwidth;
- $createheight = $srcheight*$scale;
- } else{
- $createwidth = $srcwidth;
- $createheight = $srcheight;
- }
- }
- $psrc_x = $psrc_y = 0;
- $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //Find different image processing functions
- $srcimg = $createfun($image); //Return the image identifier
- if($type != 'gif' && function_exists('imagecreatetruecolor')){
- $ thumbimg = imagecreatetruecolor($createwidth, $createheight); //Create a new true color image
- }else{
- $thumbimg = imagecreate($createwidth, $createheight); //Create a new palette-based image
- }
- if( function_exists('imagecopyresampled')){ //Resample copy part of the image and resize, true color
- //imagecopyresampled(new image, source image, x distance from the upper left corner of the new image, y distance from the upper left corner of the new image, upper left corner of the source image x distance, y distance from the upper left corner of the source image, new image width, new image height, source image width, source image height)
- imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight , $srcwidth, $srcheight);
-
- }else{ //Copy part of the image and resize, palette
- imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheight);
- }
- if($type=='gif' || $type=='png')
- {
- //imagecolorallocate assigns a color to an image
- $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // Fill the background color of the palette-based image, assign a green
- // imagecolortransparent defines a color as transparent
- imagecolortransparent($thumbimg, $background_color); // Set to transparent Color, if this line is commented out, a green image will be output
- }
- // imageinterlace activates or disables interlacing
- if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this ->interlace);
- $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
- //imagejpeg imagegif imagepng
- $filename = substr($image, 0, strrpos( $image, '.')).$suffix.'.'.$type; //Get the file name
- //aaa.gif aaa_thumb.gif
- $imagefun($thumbimg, $filename); //New image
- imagedestroy ($thumbimg); //Destroy the thumbnail
- imagedestroy($srcimg); //Destroy the source image
- return $filename;
- }
- /**
- * Generate thumbnails of specific sizes to solve the problem that the original thumbnails cannot meet the specific size. PS: The parts of the image that do not meet the thumbnail ratio will be cropped.
- * @static
- * @access public
- * @param string $image Original image
- * @param string $type image format
- * @param string $thumbname thumbnail file name
- * @param string $maxWidth width
- * @param string $maxHeight height
- * @param boolean $interlace enable interlacing
- * @return void
- */
- static function thumb2($image, $thumbname, $ type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
- // Get original image information
- $info = Image::getImageInfo($image);
- if ($info !== false ) {
- $srcWidth = $info['width'];
- $srcHeight = $info['height'];
- $type = empty($type) ? $info['type'] : $type;
- $type = strtolower($type);
- $interlace = $interlace ? 1 : 0;
- unset($info);
- $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // Calculate the scaling ratio
- //Judge the ratio between the original image and the thumbnail. For example, if the original image is wider than the thumbnail, crop both sides and vice versa..
- if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
- //Higher than
- $srcX = 0 ;
- $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
- $cutWidth = $srcWidth;
- $cutHeight = $maxHeight / $scale;
- }else{
- //Wider than
- $srcX = ($ srcWidth - $maxWidth / $scale) / 2;
- $srcY = 0;
- $cutWidth = $maxWidth / $scale;
- $cutHeight = $srcHeight;
- }
-
- // 载入原图
- $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
- $srcImg = $createFun($image);
-
- //创建缩略图
- if ($type != 'gif' && function_exists('imagecreatetruecolor'))
- $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
- else
- $thumbImg = imagecreate($maxWidth, $maxHeight);
-
- // 复制图片
- if (function_exists("ImageCopyResampled"))
- imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
- else
- imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
- if ('gif' == $type || 'png' == $type) {
- //imagealphablending($thumbImg, false);//取消默认的混色模式
- //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
- $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
- imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
- }
-
- // 对jpeg图形设置隔行扫描
- if ('jpg' == $type || 'jpeg' == $type)
- imageinterlace($thumbImg, $interlace);
-
- // 生成图片
- $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
- @chmod(dirname($thumbname),0777);
- $imageFun($thumbImg, $thumbname);
- imagedestroy($thumbImg);
- imagedestroy($srcImg);
- return $thumbname;
- }
- return false;
- }
-
-
- /**
- * Get image information
- * @static
- * @access public
- * @param string $image image file name
- * @return mixed
- */
-
- static function getImageInfo($img) {
- $imageInfo = getimagesize($img);
- if ($imageInfo !== false) {
- $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
- $imageSize = filesize($img);
- $info = array(
- "width" => $imageInfo[0],
- "height" => $imageInfo[1],
- "type" => $imageType,
- "size" => $imageSize,
- "mime" => $imageInfo['mime']
- );
- return $info;
- } else {
- return false;
- }
- }
-
- //watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
- function watermark($source, $target = '', $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc',$x='',$y='')
- {
- if( !$this->check($source)) return false;
- if(!$target) $target = $source;
- if ($w_img == '' && $w_text == '')
- $w_img = $this->w_img;
- $source_info = getimagesize($source);
- $source_w = $source_info[0]; //获取宽
- $source_h = $source_info[1]; //获取高
- if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //宽和高达不到要求直接返回
- switch($source_info[2]) //新建图片
- {
- case 1 :
- $source_img = imagecreatefromgif($source);
- break;
- case 2 :
- $source_img = imagecreatefromjpeg($source);
- break;
- case 3 :
- $source_img = imagecreatefrompng($source);
- break;
- default :
- return false;
- }
- if(!empty($w_img) && file_exists($w_img)) //水印文件
- {
- $ifwaterimage = 1; //是否水印图
- $water_info = getimagesize($w_img); //水印信息
- $width = $water_info[0];
- $height = $water_info[1];
-
- switch($water_info[2])
- {
- case 1 :
- $water_img = imagecreatefromgif($w_img);
-
- break;
- case 2 :
- $water_img = imagecreatefromjpeg($w_img);
- break;
- case 3 :
- $water_img = imagecreatefrompng($w_img);
-
- break;
- default :
- return;
- }
- }
- else
- {
- $ifwaterimage = 0;
- //imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
- //imagettfbbox (font size, font angle, font file, file)
- // echo $this->fontfile;
- $temp = imagettfbbox(ceil($w_font*1.6), 0, $this->fontfile, $w_text);//Get the range of text using truetype font
- $width = $temp[4] - $temp[6]; //The X position of the upper right corner - the X position of the upper left corner
- $height = $temp[3] - $temp[5]; //Lower right corner Y position - Upper right corner Y position
- unset($temp);
- }
- switch($w_pos)
- {
- case 0: //Random position
- $wx = rand(0 ,($source_w - $width));
- $wy = rand(0,($source_h - $height));
- break;
- case 1: //Upper left corner
- $wx = 25;
- $wy = 25;
- break;
- case 2: //The upper middle position
- $wx = ($source_w - $width) / 2;
- $wy = 0;
- break;
- case 3: //The upper right corner
- $wx = $source_w - $width;
- $wy = 0;
- break;
- case 4: //Middle position on the left
- $wx = 0;
- $wy = ($source_h - $height) / 2;
- break;
- case 5: // Middle position
- $wx = ($source_w - $width) / 2;
- $wy = ($source_h - $height) / 2;
- break;
- case 6: // Bottom middle position
- $wx = ($source_w - $width) / 2;
- $wy = $source_h - $height;
- break;
- case 7: //Lower left corner
- $wx = 0;
- $wy = $source_h - $height;
- break;
- case 8: //Middle position on the right
- $wx = $source_w - $width;
- $wy = ($source_h - $height) /2;
- break;
- case 9: //Lower right corner
- $wx = $source_w - $width;
- $wy = $source_h - $height ;
- break;
- default: //random
- $wx = rand(0,($source_w - $width));
- $wy = rand(0,($source_h - $height ));
- break;
- case 10://Customized position
- $wx = $x;
- $wy = $y;
- case 11: //Bottom middle position--for craftsmen only
- $wx = ($source_w - $width) / 2;
- $wy = $source_h - $height-50;
- break;
- break;
- }
- if($ifwaterimage) //If there is a watermark image
- {
-
- //imagecopymerge copy and merge images Part of
- //Parameters (source image, watermark image, copy to source image x position, copy to source image y position, from watermark image x position, from watermark image y position, height, width, transparency)
- //imagecopymerge( $source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
-
- imagecopy($source_img,$water_img,$wx,$wy,0,0, $width,$height);
- }
- else
- {
- if(!empty($w_color) && (strlen($w_color)==7))
- {
- $r = hexdec(substr($w_color,1,2 )); //Get red
- $g = hexdec(substr($w_color,3,2)); //Get green
- $b = hexdec(substr($w_color,5)); //Get blue
- }
- else
- {
- return;
- }
- //imagecolorallocate fills the background color with an image based on the palette
- //imagestring draws a line of string horizontally
- //imagestring(source image, font size, position X, position Y, text , color)
- //Parameters ($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
- imagettftext($source_img,$w_font,0, $wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
- //imagestring($source_img,$w_font,$wx,$wy,$ w_text,imagecolorallocate($source_img,$r,$g,$b));
- }
-
- //Output to a file or browser
- switch($source_info[2])
- {
- case 1 :
- imagegif($source_img, $target); //Output the image to a browser or file in GIF format
- break ;
- case 2 :
- imagejpeg($source_img, $target, $this->w_quality); //Output the image to the browser or file in JPEG format
- break;
- case 3 :
- imagepng($source_img, $target ; if(isset($water_img))
- {
- imagedestroy($water_img); //Destroy
- }
- unset($source_info);
- imagedestroy($source_img);
- return true;
- }
- //gd library must exist, The suffix is jpg|jpeg|gif|png, the file exists, imagecreatefromjpeg or imagecreatefromgif exists
- function check($image)
- {
- return extension_loaded('gd') &&
- preg_match("/.(JPG|JPEG|PNG|GIF| jpg|jpeg|gif|png)/i", $image, $m) &&
- file_exists($image) &&
- function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $ m[1]));
- //imagecreatefromjpeg
- //imagecreatefromgif
- //imagecreatefrompng
- }
- }
-
- /**
- Thumbnail
- 1. Create a new image resource through imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
- 2.imagecopyresampled Copy the image and resize it
-
- Watermark: image watermark, text watermark
- 1. Create image
- 2. Add watermark
- Image watermark: imagecopymerge Put 2 Merge images together
- Text watermark: imagettftext Write text to images
-
-
- */
- error_reporting(0);
- $hostdir = dirname(__FILE__);
- $ filesnames = scandir($hostdir.'/desktop');
- $img = new image();
- foreach($filesnames as $name){
- if($name != '.' && $name != '.. '){
- $imgPath = $hostdir.'/desktop/';
- $imgsize = getimagesize($imgPath.$name);
- $imgInfo = explode('.',$name);
- $imginfo2 = explode(' _',$imgInfo[0]);
- if($imginfo2[count($imginfo2)-1] != 'small'){
- $img_small = $imgPath.$imgInfo[0].'_small.'.$ imgInfo[1];
- $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);
- }
- }
- }
-
-
- Copy code
-
-
-
-
-
-
Equal proportions, PHP
|