Chinaunix首页 | 论坛 | 博客
  • 博客访问: 344366
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 223
  • 用 户 组: 普通用户
  • 注册时间: 2020-07-01 21:01
个人简介

青岛风月网www.qdspaw.com,男士减压休闲养生体验平台。

文章分类
文章存档

2020年(21)

我的朋友

分类: PHP

2020-07-02 10:39:16

如果做过那种门户站的朋友,肯定知道,一张图片可能会在不同的地方显示、大小不同、比例也不同,如果只用一张图的话,那么肯定会变形,而且在显示小图的地方,链接大图,又太浪费了.....用缩略图来处理,也不完美,因为每个地方出现的比例大小可能都不一样 。
php自动裁切相比你们看到过类似那种图片地址/aaaa/abc_200_100.jpg 或者/aaaa/abc_200*100.jpg
我的方式就是在需要图片地方把这个图片地址转化为类似上面的那种地址, 然后通过apache 的rewrite 定向到一个处理程序。根据宽高生成一个图片然后保存起来,在不动原图的任何信息和位置的情况下对图片做处理。

  1. <?php
  2. $url = $_GET['url'];
  3. $src = '' . preg_replace('/_(\d+)_(\d+)/', '', $url);
  4. $filename = './data/attachment/' . $url;
  5. if (file_exists($filename)) {
  6. ob_start();
  7. header('Content-type:image/jpeg');
  8. readfile($filename);
  9. ob_flush();
  10. flush();
  11. } else {
  12. if(!preg_match('/_(\d+)_(\d+)/', $url, $wh)){
  13. defulat();
  14. exit();
  15. }
  16. $width = $wh[1];
  17. $height = $wh[2];
  18. thumb(realpath($src), $width, $height, $filename, 'crop', '85');
  19. }
  20. function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {
  21. try {
  22. $imageValue = getimagesize($src);
  23. $sourceWidth = $imageValue[0]; //原图宽
  24. $sourceHeight = $imageValue[1]; //原图高
  25. $thumbWidth = $width; //缩略图宽
  26. $thumbHeight = $height; //缩略图高
  27. $_x = 0;
  28. $_y = 0;
  29. $w = $sourceWidth;
  30. $h = $sourceHeight;
  31. if ($mode == 'scale') {
  32. if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {
  33. $_x = floor(($thumbWidth - $sourceWidth) / 2);
  34. $_y = floor(($thumbHeight - $sourceHeight) / 2);
  35. $thumbWidth = $sourceWidth;
  36. $thumbHeight = $sourceHeight;
  37. } else {
  38. if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {
  39. $thumbHeight = floor($sourceHeight * $width / $sourceWidth);
  40. $_y = floor(($height - $thumbHeight) / 2);
  41. } else {
  42. $thumbWidth = floor($sourceWidth * $height / $sourceHeight);
  43. $_x = floor(($width - $thumbWidth) / 2);
  44. }
  45. }
  46. } else if ($mode == 'crop') {
  47. if ($sourceHeight < $thumbHeight) { //如果原图尺寸小于当前尺寸
  48. $thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);
  49. $thumbHeight = $sourceHeight;
  50. }
  51. if ($sourceWidth < $thumbWidth) {
  52. $thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);
  53. $thumbWidth = $sourceWidth;
  54. }
  55. $s1 = $sourceWidth / $sourceHeight; //原图比例
  56. $s2 = $width / $height; //新图比例
  57. if ($s1 == $s2) {
  58. } else if ($s1 > $s2) { //全高度
  59. $y = 0;
  60. $ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));
  61. $x = ($ax - $thumbWidth) / 2;
  62. $w = $thumbWidth / ($thumbHeight / $sourceHeight);
  63. } else { //全宽度
  64. $x = 0;
  65. $ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模拟原图比例高度
  66. $y = ($ay - $thumbHeight) / 2;
  67. $h = $thumbHeight / ($thumbWidth / $sourceWidth);
  68. }
  69. }
  70. switch ($imageValue[2]) {
  71. case 2: $source = imagecreatefromjpeg($src);
  72. break;
  73. case 1: $source = imagecreatefromgif($src);
  74. break;
  75. case 3: $source = imagecreatefrompng($src);
  76. break;
  77. case 6: $source = imagecreatefromwbmp($src);
  78. break;
  79. default: defulat();
  80. return;
  81. }
  82. header("Content-type: image/jpeg");
  83. $thumb = imagecreatetruecolor($width, $height);
  84. imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));
  85. imagecopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);
  86. imagejpeg($thumb, null, $quality);
  87. // if ($_SERVER['HTTP_REFERER'] || false !== stripos($_SERVER['HTTP_REFERER'], 'http://' . $_SERVER['SERVER_NAME'])) {
  88. imagejpeg($thumb, $filename, $quality);
  89. // }
  90. imagedestroy($thumb);
  91. imagedestroy($source);
  92. } catch (Exception $ex) {
  93. defulat();
  94. }
  95. }
  96. function defulat() {
  97. $default_img = realpath('media/images/nopic.jpg');
  98. ob_start();
  99. header('Content-type:image/jpeg');
  100. readfile($default_img);
  101. ob_flush();
  102. flush();
  103. }


阅读(799) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~