Home > Backend Development > PHP Tutorial > PHP folder traversal, image compression at equal proportions

PHP folder traversal, image compression at equal proportions

WBOY
Release: 2016-07-25 08:42:54
Original
1195 people have browsed it
  1. /**
  2. * Source: Internet
  3. */
  4. class image
  5. {
  6. var $w_pct = 100; //Transparency
  7. var $w_quality = 100; //Quality
  8. var $w_minwidth = 500; / /Minimum width
  9. var $w_minheight = 500; //Minimum height
  10. var $interlace = 0; //Whether the image is interlaced
  11. var $fontfile; //Font file
  12. var $w_img; //Default watermark image
  13. function __construct()
  14. {
  15. $this->fontfile = $_SERVER['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';
  16. $this->w_img = '';
  17. }
  18. function image( )
  19. {
  20. $this->__construct();
  21. }
  22. function info($img)
  23. {
  24. $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"",
  25. if($imageinfo === false) return false ;
  26. $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //Get the image file type $imageinfo[2] is the mark of the image type
  27. $imagesize = filesize($img); // Image size
  28. $info = array(
  29. 'width'=>$imageinfo[0],
  30. 'height'=>$imageinfo[1],
  31. 'type'=>$imagetype,
  32. 'size'=> ;$imagesize,
  33. 'mime'=>$imageinfo['mime']
  34. );
  35. return $info;
  36. }
  37. /**
  38. * 缩略图
  39. *
  40. * @param string $image
  41. * @param string $filename
  42. * @param int $maxwidth
  43. * @param int $maxheight
  44. * @param string $suffix
  45. * @param int $autocut
  46. * @return string
  47. */
  48. function thumb($image, $filename = '' ,$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)
  49. {
  50. if( !$this->check($image)) return false;
  51. $info = $this- >info($image); //Get image information
  52. if($info === false) return false;
  53. $srcwidth = $info['width']; //Source image width
  54. $srcheight = $info[ 'height']; //The source image is high
  55. $pathinfo = pathinfo($image);
  56. $type = $pathinfo['extension']; //Get the extension
  57. if(!$type) $type = $info[ 'type']; //If not obtained, use $info['type']
  58. $type = strtolower($type);
  59. unset($info);
  60. $scale = min($maxwidth/$srcwidth, $ maxheight/$srcheight); //Get the thumbnail ratio
  61. //Get the ratio according to the source image
  62. $createwidth = $width = (int) ($srcwidth*$scale); //Get the thumbnail width();
  63. $createheight = $height = (int)($srcheight*$scale);//(); //Get the thumbnail height
  64. $psrc_x = $psrc_y = 0;
  65. if($autocut) //According to the proportion of the thumbnail To get
  66. {
  67. if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //If the thumbnail is proportionally narrower than the source image
  68. {
  69. $width = $maxheight/$height *$width; //The width is processed according to the corresponding proportion
  70. $height = $maxheight;// //The height remains unchanged
  71. }
  72. elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) //If the thumbnail is proportionally wider than the source image
  73. {
  74. $height = $maxwidth/$width*$height;//
  75. $width = $maxwidth;
  76. }
  77. if($maxwidth == '55') {
  78. $width = '55';
  79. $createwidth = '55';
  80. }
  81. if($maxwidth == '110'){
  82. $width = '110';
  83. $createwidth = '110';
  84. }
  85. }
  86. if($srcwidth<='280' && $maxwidth != '110'){
  87. $createwidth= $srcwidth;
  88. $createheight = $srcheight;
  89. }
  90. $createfun = 'imagecreatefrom'.($type= ='jpg' ? 'jpeg' : $type); //Find different image processing functions
  91. $srcimg = $createfun($image); //Return the image identifier
  92. if($type != 'gif' && function_exists ('imagecreatetruecolor')){
  93. $thumbimg = imagecreatetruecolor($createwidth, $createheight); //Create a new true color image
  94. }else{
  95. if($maxwidth == '110' && $srcheight >= $srcwidth){
  96. $height1 = 72;
  97. $thumbimg = imagecreate($width, $height1); //Create a new palette-based Image
  98. }elseif ($maxwidth == '110' && $srcheight <= $srcwidth){
  99. $width1 = 110;
  100. $thumbimg = imagecreate($width1, $height); //Create a new palette-based Image
  101. }else{
  102. $thumbimg = imagecreate($width, $height); //Create a new palette-based image
  103. }
  104. }
  105. if(function_exists('imagecopyresampled')){ //Resample and copy part of the image And adjust the size, true color
  106. //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)
  107. if($maxwidth == '110' && $srcheight >= $srcwidth){
  108. $psrc_x = 0;
  109. $psrc_yz = $height/2;
  110. $psrc_y = $psrc_yz;
  111. $width = '110';
  112. }
  113. if($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth <= '110'){
  114. $psrc_x = 0;
  115. $psrc_yz = $height/2;
  116. $psrc_y = $psrc_yz;
  117. $width = '110';
  118. }
  119. if($maxheight == '72' && $srcheight <= $srcwidth && $srcheight<= '72' && $srcwidth >= '110'){
  120. $cha = ($maxheight-$srcheight)/2;
  121. $psrc_y = -($cha);
  122. $width = $srcwidth;
  123. $height = '72';
  124. $srcheight = '72';
  125. }
  126. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  127. }else{ //Copy part of the image and resize, palette
  128. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  129. }
  130. if($type=='gif' || $type=='png')
  131. {
  132. //imagecolorallocate assigns a color to an image
  133. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); / / Fill the background color of the image based on the palette, assign a green
  134. // imagecolortransparent Define a color as a transparent color
  135. imagecolortransparent($thumbimg, $background_color); // Set to a transparent color, if this line is commented out Output a green image
  136. }
  137. // imageinterlace activates or disables interlacing
  138. if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  139. $ imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  140. //imagejpeg imagegif imagepng
  141. if(empty($filename)) $filename = substr($image, 0, strrpos( $image, '.')).$suffix.'.'.$type; //Get the file name
  142. //aaa.gif aaa_thumb.gif
  143. $imagefun($thumbimg, $filename); //New image
  144. imagedestroy ($thumbimg); //Destroy the thumbnail
  145. imagedestroy($srcimg); //Destroy the source image
  146. return $filename;
  147. }
  148. /**
  149. * Limit width or height
  150. *
  151. * @param string $image
  152. * @param int $maxwidth
  153. * @param int $maxheight
  154. * @param string $suffix
  155. * @return string
  156. */
  157. function thumb3($image,$maxwidth = 0, $maxheight = 0, $suffix='_thumb')
  158. {
  159. if( !$this->check($image)) return false;
  160. $info = $this->info($image); //Get Image information
  161. if($info === false) return false;
  162. $srcwidth = $info['width']; //Source image width
  163. $srcheight = $info['height']; //Source image height
  164. $pathinfo = pathinfo($image);
  165. $type = $pathinfo['extension']; //Get the extension
  166. if(!$type) $type = $info['type']; //If not obtained , use $info['type']
  167. $type = strtolower($type);
  168. unset($info);
  169. if($maxwidth==0){
  170. $scale = $maxheight/$srcheight;
  171. if($ scale<1){
  172. $createwidth = $srcwidth*$scale;
  173. $createheight = $maxheight;
  174. }else{
  175. $createwidth = $srcwidth;
  176. $createheight = $srcheight;
  177. }
  178. }
  179. if($maxheight==0){
  180. $scale = $maxwidth/$srcwidth;
  181. if($scale<1){
  182. $createwidth = $maxwidth;
  183. $createheight = $srcheight*$scale;
  184. } else{
  185. $createwidth = $srcwidth;
  186. $createheight = $srcheight;
  187. }
  188. }
  189. $psrc_x = $psrc_y = 0;
  190. $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //Find different image processing functions
  191. $srcimg = $createfun($image); //Return the image identifier
  192. if($type != 'gif' && function_exists('imagecreatetruecolor')){
  193. $ thumbimg = imagecreatetruecolor($createwidth, $createheight); //Create a new true color image
  194. }else{
  195. $thumbimg = imagecreate($createwidth, $createheight); //Create a new palette-based image
  196. }
  197. if( function_exists('imagecopyresampled')){ //Resample copy part of the image and resize, true color
  198. //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)
  199. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight , $srcwidth, $srcheight);
  200. }else{ //Copy part of the image and resize, palette
  201. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheight);
  202. }
  203. if($type=='gif' || $type=='png')
  204. {
  205. //imagecolorallocate assigns a color to an image
  206. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // Fill the background color of the palette-based image, assign a green
  207. // imagecolortransparent defines a color as transparent
  208. imagecolortransparent($thumbimg, $background_color); // Set to transparent Color, if this line is commented out, a green image will be output
  209. }
  210. // imageinterlace activates or disables interlacing
  211. if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this ->interlace);
  212. $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  213. //imagejpeg imagegif imagepng
  214. $filename = substr($image, 0, strrpos( $image, '.')).$suffix.'.'.$type; //Get the file name
  215. //aaa.gif aaa_thumb.gif
  216. $imagefun($thumbimg, $filename); //New image
  217. imagedestroy ($thumbimg); //Destroy the thumbnail
  218. imagedestroy($srcimg); //Destroy the source image
  219. return $filename;
  220. }
  221. /**
  222. * 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.
  223. * @static
  224. * @access public
  225. * @param string $image Original image
  226. * @param string $type image format
  227. * @param string $thumbname thumbnail file name
  228. * @param string $maxWidth width
  229. * @param string $maxHeight height
  230. * @param boolean $interlace enable interlacing
  231. * @return void
  232. */
  233. static function thumb2($image, $thumbname, $ type='', $maxWidth=200, $maxHeight=50, $interlace=true) {
  234. // Get original image information
  235. $info = Image::getImageInfo($image);
  236. if ($info !== false ) {
  237. $srcWidth = $info['width'];
  238. $srcHeight = $info['height'];
  239. $type = empty($type) ? $info['type'] : $type;
  240. $type = strtolower($type);
  241. $interlace = $interlace ? 1 : 0;
  242. unset($info);
  243. $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // Calculate the scaling ratio
  244. //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..
  245. if($maxWidth / $srcWidth > $maxHeight / $srcHeight){
  246. //Higher than
  247. $srcX = 0 ;
  248. $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;
  249. $cutWidth = $srcWidth;
  250. $cutHeight = $maxHeight / $scale;
  251. }else{
  252. //Wider than
  253. $srcX = ($ srcWidth - $maxWidth / $scale) / 2;
  254. $srcY = 0;
  255. $cutWidth = $maxWidth / $scale;
  256. $cutHeight = $srcHeight;
  257. }
  258. // 载入原图
  259. $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);
  260. $srcImg = $createFun($image);
  261. //创建缩略图
  262. if ($type != 'gif' && function_exists('imagecreatetruecolor'))
  263. $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);
  264. else
  265. $thumbImg = imagecreate($maxWidth, $maxHeight);
  266. // 复制图片
  267. if (function_exists("ImageCopyResampled"))
  268. imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  269. else
  270. imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);
  271. if ('gif' == $type || 'png' == $type) {
  272. //imagealphablending($thumbImg, false);//取消默认的混色模式
  273. //imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
  274. $background_color = imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色
  275. imagecolortransparent($thumbImg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  276. }
  277. // 对jpeg图形设置隔行扫描
  278. if ('jpg' == $type || 'jpeg' == $type)
  279. imageinterlace($thumbImg, $interlace);
  280. // 生成图片
  281. $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);
  282. @chmod(dirname($thumbname),0777);
  283. $imageFun($thumbImg, $thumbname);
  284. imagedestroy($thumbImg);
  285. imagedestroy($srcImg);
  286. return $thumbname;
  287. }
  288. return false;
  289. }
  290. /**
  291. * Get image information
  292. * @static
  293. * @access public
  294. * @param string $image image file name
  295. * @return mixed
  296. */
  297. static function getImageInfo($img) {
  298. $imageInfo = getimagesize($img);
  299. if ($imageInfo !== false) {
  300. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));
  301. $imageSize = filesize($img);
  302. $info = array(
  303. "width" => $imageInfo[0],
  304. "height" => $imageInfo[1],
  305. "type" => $imageType,
  306. "size" => $imageSize,
  307. "mime" => $imageInfo['mime']
  308. );
  309. return $info;
  310. } else {
  311. return false;
  312. }
  313. }
  314. //watermark(源图,生成文件,生成位置,水印文件,水印文本,背景色)
  315. function watermark($source, $target = '', $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc',$x='',$y='')
  316. {
  317. if( !$this->check($source)) return false;
  318. if(!$target) $target = $source;
  319. if ($w_img == '' && $w_text == '')
  320. $w_img = $this->w_img;
  321. $source_info = getimagesize($source);
  322. $source_w = $source_info[0]; //获取宽
  323. $source_h = $source_info[1]; //获取高
  324. if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //宽和高达不到要求直接返回
  325. switch($source_info[2]) //新建图片
  326. {
  327. case 1 :
  328. $source_img = imagecreatefromgif($source);
  329. break;
  330. case 2 :
  331. $source_img = imagecreatefromjpeg($source);
  332. break;
  333. case 3 :
  334. $source_img = imagecreatefrompng($source);
  335. break;
  336. default :
  337. return false;
  338. }
  339. if(!empty($w_img) && file_exists($w_img)) //水印文件
  340. {
  341. $ifwaterimage = 1; //是否水印图
  342. $water_info = getimagesize($w_img); //水印信息
  343. $width = $water_info[0];
  344. $height = $water_info[1];
  345. switch($water_info[2])
  346. {
  347. case 1 :
  348. $water_img = imagecreatefromgif($w_img);
  349. break;
  350. case 2 :
  351. $water_img = imagecreatefromjpeg($w_img);
  352. break;
  353. case 3 :
  354. $water_img = imagecreatefrompng($w_img);
  355. break;
  356. default :
  357. return;
  358. }
  359. }
  360. else
  361. {
  362. $ifwaterimage = 0;
  363. //imagettfbbox 本函数计算并返回一个包围着 TrueType 文本范围的虚拟方框的像素大小。
  364. //imagettfbbox (font size, font angle, font file, file)
  365. // echo $this->fontfile;
  366. $temp = imagettfbbox(ceil($w_font*1.6), 0, $this->fontfile, $w_text);//Get the range of text using truetype font
  367. $width = $temp[4] - $temp[6]; //The X position of the upper right corner - the X position of the upper left corner
  368. $height = $temp[3] - $temp[5]; //Lower right corner Y position - Upper right corner Y position
  369. unset($temp);
  370. }
  371. switch($w_pos)
  372. {
  373. case 0: //Random position
  374. $wx = rand(0 ,($source_w - $width));
  375. $wy = rand(0,($source_h - $height));
  376. break;
  377. case 1: //Upper left corner
  378. $wx = 25;
  379. $wy = 25;
  380. break;
  381. case 2: //The upper middle position
  382. $wx = ($source_w - $width) / 2;
  383. $wy = 0;
  384. break;
  385. case 3: //The upper right corner
  386. $wx = $source_w - $width;
  387. $wy = 0;
  388. break;
  389. case 4: //Middle position on the left
  390. $wx = 0;
  391. $wy = ($source_h - $height) / 2;
  392. break;
  393. case 5: // Middle position
  394. $wx = ($source_w - $width) / 2;
  395. $wy = ($source_h - $height) / 2;
  396. break;
  397. case 6: // Bottom middle position
  398. $wx = ($source_w - $width) / 2;
  399. $wy = $source_h - $height;
  400. break;
  401. case 7: //Lower left corner
  402. $wx = 0;
  403. $wy = $source_h - $height;
  404. break;
  405. case 8: //Middle position on the right
  406. $wx = $source_w - $width;
  407. $wy = ($source_h - $height) /2;
  408. break;
  409. case 9: //Lower right corner
  410. $wx = $source_w - $width;
  411. $wy = $source_h - $height ;
  412. break;
  413. default: //random
  414. $wx = rand(0,($source_w - $width));
  415. $wy = rand(0,($source_h - $height ));
  416. break;
  417. case 10://Customized position
  418. $wx = $x;
  419. $wy = $y;
  420. case 11: //Bottom middle position--for craftsmen only
  421. $wx = ($source_w - $width) / 2;
  422. $wy = $source_h - $height-50;
  423. break;
  424. break;
  425. }
  426. if($ifwaterimage) //If there is a watermark image
  427. {
  428. //imagecopymerge copy and merge images Part of
  429. //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)
  430. //imagecopymerge( $source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  431. imagecopy($source_img,$water_img,$wx,$wy,0,0, $width,$height);
  432. }
  433. else
  434. {
  435. if(!empty($w_color) && (strlen($w_color)==7))
  436. {
  437. $r = hexdec(substr($w_color,1,2 )); //Get red
  438. $g = hexdec(substr($w_color,3,2)); //Get green
  439. $b = hexdec(substr($w_color,5)); //Get blue
  440. }
  441. else
  442. {
  443. return;
  444. }
  445. //imagecolorallocate fills the background color with an image based on the palette
  446. //imagestring draws a line of string horizontally
  447. //imagestring(source image, font size, position X, position Y, text , color)
  448. //Parameters ($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
  449. imagettftext($source_img,$w_font,0, $wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);
  450. //imagestring($source_img,$w_font,$wx,$wy,$ w_text,imagecolorallocate($source_img,$r,$g,$b));
  451. }
  452. //Output to a file or browser
  453. switch($source_info[2])
  454. {
  455. case 1 :
  456. imagegif($source_img, $target); //Output the image to a browser or file in GIF format
  457. break ;
  458. case 2 :
  459. imagejpeg($source_img, $target, $this->w_quality); //Output the image to the browser or file in JPEG format
  460. break;
  461. case 3 :
  462. imagepng($source_img, $target ; if(isset($water_img))
  463. {
  464. imagedestroy($water_img); //Destroy
  465. }
  466. unset($source_info);
  467. imagedestroy($source_img);
  468. return true;
  469. }
  470. //gd library must exist, The suffix is ​​jpg|jpeg|gif|png, the file exists, imagecreatefromjpeg or imagecreatefromgif exists
  471. function check($image)
  472. {
  473. return extension_loaded('gd') &&
  474. preg_match("/.(JPG|JPEG|PNG|GIF| jpg|jpeg|gif|png)/i", $image, $m) &&
  475. file_exists($image) &&
  476. function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $ m[1]));
  477. //imagecreatefromjpeg
  478. //imagecreatefromgif
  479. //imagecreatefrompng
  480. }
  481. }
  482. /**
  483. Thumbnail
  484. 1. Create a new image resource through imagecreatefromgif imagecreatefromjpeg imagecreatefrompng
  485. 2.imagecopyresampled Copy the image and resize it
  486. Watermark: image watermark, text watermark
  487. 1. Create image
  488. 2. Add watermark
  489. Image watermark: imagecopymerge Put 2 Merge images together
  490. Text watermark: imagettftext Write text to images
  491. */
  492. error_reporting(0);
  493. $hostdir = dirname(__FILE__);
  494. $ filesnames = scandir($hostdir.'/desktop');
  495. $img = new image();
  496. foreach($filesnames as $name){
  497. if($name != '.' && $name != '.. '){
  498. $imgPath = $hostdir.'/desktop/';
  499. $imgsize = getimagesize($imgPath.$name);
  500. $imgInfo = explode('.',$name);
  501. $imginfo2 = explode(' _',$imgInfo[0]);
  502. if($imginfo2[count($imginfo2)-1] != 'small'){
  503. $img_small = $imgPath.$imgInfo[0].'_small.'.$ imgInfo[1];
  504. $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);
  505. }
  506. }
  507. }
  508. Copy code
Equal proportions, PHP

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template