调整图像大小并命名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调整图像大小并命名相关的知识,希望对你有一定的参考价值。

This properly handles transparencies for PNG/GIF.
  1. /**
  2.  * Resizes and renames an image file
  3.  * @return resource An image identifier
  4.  * @return false on error
  5.  * @param string $source Path to source image
  6.  * @param string $dest Path to output image
  7.  * @param int $n_height New height to set image to
  8.  * @param int $n_width New width to set image to
  9.  * @param bool $overwrite Allow overwrite of image file
  10.  * @author Glen Solsberry <[email protected]>
  11.  */
  12. function resize_and_name_image($source, $dest, $n_height, $n_width, $overwrite = false) {
  13. if (!file_exists($source)) { error_log("$source doesn't exist"); return false; }
  14. if (file_exists($dest) && !$overwrite) { error_log("$dest exists, and we're not overwriting"); return false; }
  15. list($width, $height, $type, $attr) = getimagesize($source);
  16. if ($width > $height) {
  17. $n_height = ($n_width / $width) * $height;
  18. } else {
  19. $n_width = ($n_height / $height) * $width;
  20. }
  21.  
  22. $mime_type = image_type_to_mime_type($type);
  23. $mime_type = preg_replace("/^.*?//", "", $mime_type);
  24.  
  25. // call the appropriate imagecreatefrom function for this image
  26. if (function_exists("imagecreatefrom" . $mime_type)) {
  27. $img = call_user_func("imagecreatefrom" . $mime_type, $source);
  28. } else {
  29. display_error("invalid_image_format");
  30. error_log("imagecreatefrom{$mime_type} doesn't exist");
  31. return false;
  32. }
  33.  
  34. $new_img = imagecreatetruecolor($n_width, $n_height);
  35. // imagecolorallocatealpha($new_img, 0, 0, 0, 0);
  36.  
  37. // get and reallocate transparency-color
  38. $transindex = imagecolortransparent($img);
  39. if ($transindex >= 0) {
  40. $transcol = imagecolorsforindex($img, $transindex);
  41. $transindex = imagecolorallocatealpha($new_img, $transcol['red'], $transcol['green'], $transcol['blue'], 127);
  42. imagefill($new_img, 0, 0, $transindex);
  43. } else if ($transindex == -1) {
  44. imagecolortransparent($new_img, imagecolorallocate($new_img, 0, 0, 0));
  45. imagealphablending($new_img, false);
  46. imagesavealpha($new_img, true);
  47. }
  48.  
  49. imagecopyresampled($new_img, $img, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
  50.  
  51. // stuff the existing file off in to a backup directory
  52. if (function_exists("image" . $mime_type)) {
  53. // we need to get the end path
  54. $my_local_dest = preg_replace("/^(.*)/(.*?)$/", "$1/source/$2", $dest);
  55. $my_local_dest = preg_replace("/^(.*)_.*/", "$1.$mime_type", $my_local_dest);
  56. // make sure that the directory exists
  57. if (! is_dir(dirname($my_local_dest))) {
  58. $res = mkdir(dirname($my_local_dest));
  59. }
  60. $res = call_user_func("image" . $mime_type, $img, $my_local_dest);
  61. if (!$res) { error_log("image{$mime_type} failed!!!!"); }
  62. }
  63.  
  64. imagedestroy($img);
  65.  
  66. // write the image out to the new file, using imagepng/imagegif/etc
  67. $my_dest_type = preg_replace("/^.*.(.*?)$/", "$1", $dest);
  68. if (function_exists("image" . $my_dest_type)) {
  69. $res = call_user_func("image" . $my_dest_type, $new_img, $dest);
  70. imagedestroy($new_img);
  71. return $res;
  72. } else {
  73. error_log("image{$my_dest_type} doesn't exist");
  74. imagedestroy($new_img);
  75. return false;
  76. }
  77.  
  78. }

以上是关于调整图像大小并命名的主要内容,如果未能解决你的问题,请参考以下文章

调整图像大小后检测公差颜色

使用 PHP GD 调整图像大小并保存图像。这段代码有啥问题?

上传前调整图像大小 - 将画布转换为文件对象

php - 调整大小并保存图像? [复制]

调整大小后通过鼠标连续调整其余图像的大小,并保持比例

nodejs从远程服务器调整大小并保存图像