获取图像文件大小、图像的尺寸、文件类型
getimagesize() 函数将测定任何 GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM 或 WBMP 图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 IMG 标记中的 height/width 文本字符串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
$arr = getimagesize('./feng.png'); //print_r($arr); echo '你是',image_type_to_mime_type($arr[2]),'类型'; $arr = getimagesize('./home.jpg'); echo '你是',image_type_to_mime_type($arr[2]),'类型'; print_r($arr); $arr = getimagesize('./01.php'); var_dump($arr); /* 你是image/png类型你是image/jpeg类型Array ( [0] => 670 [1] => 503 [2] => 2 [3] => width="670" height="503" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) */ |
图片操作类:画布四个角的坐标水印,缩略图的两边留白!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
<?php //-------------画布四个角添加水印------------- class ImageTool { public static function imageInfo($image) { if(!file_exists($image)) {//检测目录图片是否存在 return false; } $info = getimagesize($image);//获取图片信息,是一个数组.获取失败返回false if($info == false) { return false; } $img['width'] = $info[0]; $img['height'] = $info[1]; $img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1); return $img; } public static function water($dst,$water,$save=NULL,$pos=2,$alpha=50) { if(!file_exists($dst) || !file_exists($water)) {// 先保证2个图片存在 return false; } $dinfo = self::imageInfo($dst);//读取图片信息 $winfo = self::imageInfo($water);//读取图片信息 if($winfo['height'] > $dinfo['height'] || $winfo['width'] > $dinfo['width']) { return false; } $dfunc = 'imagecreatefrom' . $dinfo['ext'];//图片格式 $wfunc = 'imagecreatefrom' . $winfo['ext'];//图片格式 if(!function_exists($dfunc) || !function_exists($wfunc)) { return false; } $dim = $dfunc($dst); // 创建待操作的画布 $wim = $wfunc($water); // 创建水印画布 // 根据水印的位置 计算粘贴的坐标 switch($pos) { case 0: // 左上角 $posx = 0; $posy = 0; break; case 1: // 右上角 $posx = $dinfo['width'] - $winfo['width']; $posy = 0; break; case 3: // 左下角 $posx = 0; $posy = $dinfo['height'] - $winfo['height']; break; default: $posx = $dinfo['width'] - $winfo['width']; $posy = $dinfo['height'] - $winfo['height']; } // 加水印 imagecopymerge ($dim,$wim, $posx , $posy , 0 , 0 , $winfo['width'] , $winfo['height'] , $alpha); // 保存 if(!$save) { $save = $dst; unlink($dst); // 删除原图 } $createfunc = 'image' . $dinfo['ext']; $createfunc($dim,$save); imagedestroy($dim); imagedestroy($wim); return true; } // print_r(ImageTool::imageInfo('./home.jpg')); /* echo ImageTool::water('./home.jpg','./smallfeng.png','home1.jpg',0)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home2.jpg',1)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home3.jpg',2)?'OK':'FAIL'; echo ImageTool::water('./home.jpg','./smallfeng.png','home4.jpg',3)?'OK':'FAIL'; */ /** -------------缩略图两边留白------------- thumb 生成缩略图 等比例缩放,两边留白 **/ public static function thumb($dst,$save=NULL,$width=200,$height=200) { // 首先判断待处理的图片存不存在 $dinfo = self::imageInfo($dst); if($dinfo == false) { return false; } // 计算缩放比例 $calc = min($width/$dinfo['width'], $height/$dinfo['height']); // 创建原始图的画布 $dfunc = 'imagecreatefrom' . $dinfo['ext']; $dim = $dfunc($dst); // 创建缩略画布 $tim = imagecreatetruecolor($width,$height); // 创建白色填充缩略画布 $white = imagecolorallocate($tim,255,255,255); // 填充缩略画布 imagefill($tim,0,0,$white); // 复制并缩略 $dwidth = (int)$dinfo['width']*$calc; $dheight = (int)$dinfo['height']*$calc; $paddingx = (int)($width - $dwidth) / 2; $paddingy = (int)($height - $dheight) / 2; imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$dwidth,$dheight,$dinfo['width'],$dinfo['height']); // 保存图片 if(!$save) { $save = $dst; unlink($dst); } $createfunc = 'image' . $dinfo['ext']; $createfunc($tim,$save); imagedestroy($dim); imagedestroy($tim); return true; } } echo ImageTool::thumb('./feng.png','./feng1.png',200,200)?'OK':'FAIL'; echo ImageTool::thumb('./feng.png','./feng2.png',200,300)?'OK':'FAIL'; echo ImageTool::thumb('./feng.png','./feng3.png',300,200)?'OK':'FAIL'; |
共

我的微信
把最实用的经验,分享给最需要的读者,希望每一位来访的朋友都能有所收获!
2016年11月25日 下午2:16 1F
路过,就支持一下
2016年11月25日 下午5:06 1B
@乐赚168