php 图像生成缩略图

旧城等待, 2022-11-20 09:49 436阅读 0赞

参考自 https://blog.csdn.net/maoxinwen1/article/details/79202442

3D全景图过大(5M左右),导致一些不需要加载全景图的页面过慢,所以增加了和全景图一样名字的400X400的缩略图(20k左右),写个脚本初始化150个G的图片数据。

  1. <?php
  2. $dir= '/var/www/html/upload/siteimg3d';
  3. /** * 生成缩略图并拷贝到指定位置 * * @param [type] $file 原图片路径 dir/1.jpg * @param [type] $newPath 移动到的位置 newDir * @param string $newName 生成的缩略图名称 new.jpg * @param integer $width 宽 400 * @param integer $height 高 400 * @param integer $per 缩放比例 为0不缩放,>0忽略参数2、3的宽高 * @return string || boolean newName || false */
  4. function imageResize($file, $newPath, $newName = '', $width = 400, $height = 400, $per = 0)
  5. {
  6. if (!file_exists($file) || !$newPath) return false;
  7. $imagedata = file_get_contents($file);
  8. // 1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),
  9. // 9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
  10. list($bigWidth, $bigHight, $bigType) = getimagesizefromstring($imagedata); // 获取图像信息
  11. if ($per > 0) { // 缩放比例
  12. $width = $bigWidth * $per;
  13. $height = $bigHight * $per;
  14. }
  15. $block = imagecreatetruecolor($width, $height); // 创建缩略图画板
  16. imagealphablending($block, false); // 启用混色模式
  17. imagesavealpha($block, true); // 保存PNG alpha通道信息
  18. $bigImg = imagecreatefromstring($imagedata); // 创建原图画板
  19. imagecopyresampled($block, $bigImg, 0, 0, 0, 0, $width, $height, $bigWidth, $bigHight); // 缩放
  20. $tmpFilename = tempnam(sys_get_temp_dir(), 'image_'); // 生成临时文件名
  21. $suffix = '';
  22. switch ($bigType) { // 保存
  23. case 1:
  24. imagegif($block, $tmpFilename);
  25. $suffix = '.gif';
  26. break;
  27. case 2:
  28. imagejpeg($block, $tmpFilename);
  29. $suffix = '.jpg';
  30. break;
  31. case 3:
  32. imagepng($block, $tmpFilename);
  33. $suffix = '.png';
  34. break;
  35. }
  36. imagedestroy($block); // 销毁
  37. if (!file_exists($newPath)) CreateDir($newPath);
  38. if ($newName == '') {
  39. $newName = time() . rand(100000, 999999) . $suffix;
  40. }
  41. copy($tmpFilename, $newPath . '/' . $newName);
  42. unlink($tmpFilename);
  43. return $newName;
  44. }
  45. function CreateDir($dir, $authority = 0755)
  46. {
  47. if (!file_exists($dir)) {
  48. CreateDir(dirname($dir));
  49. mkdir($dir, $authority);
  50. }
  51. return true;
  52. }
  53. $arr = scandir($dir);
  54. $num = count($arr);
  55. $error = './error.txt';
  56. $percent = 0;
  57. foreach ($arr as $k => $v) {
  58. if (!is_numeric($v)) continue;
  59. $filesArr = scandir($dir . '/' . $v);
  60. foreach ($filesArr as $m => $n) {
  61. $_file = $dir . '/' . $v . '/' . $n;
  62. $_newFile = $dir . '/' . $v . 'small';
  63. if ($m == 0 || $m == 1) continue;
  64. $st = imageResize($_file, $_newFile, $n);
  65. if (!$st) {
  66. file_put_contents($error, $_file . PHP_EOL, FILE_APPEND);
  67. }
  68. }
  69. $a = intval($k / $num * 10000) / 100;
  70. if ($a != $percent) {
  71. $percent = $a;
  72. echo $percent . '%' . PHP_EOL;
  73. }
  74. }

发表评论

表情:
评论列表 (有 0 条评论,436人围观)

还没有评论,来说两句吧...

相关阅读