所有图像到JPG/JPeG转换器和大小调整器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了所有图像到JPG/JPeG转换器和大小调整器相关的知识,希望对你有一定的参考价值。

I edit this from tylerhall on 11/30/-1
  1. function image($file_temporary, $file_destination, $width, $height, $type = "jpg") {
  2. $type = strtolower($type);
  3. $image_size = getimagesize($file_temporary);
  4.  
  5. if($image_size[0] > $image_size[1]) {
  6. $new_width = $width;
  7. $new_height = $image_size[1] * ($new_width / $image_size[0]);
  8. }
  9. else {
  10. $new_height = $height;
  11. $new_width = $image_size[0] * ($new_height / $image_size[1]);
  12. }
  13.  
  14. switch(image_type_to_mime_type($image_size[2])) {
  15. case "image/jpeg":
  16. $image = imagecreatefromjpeg($file_temporary);
  17. break;
  18. case "image/gif":
  19. $image = imagecreatefromgif($file_temporary);
  20. break;
  21. case "image/png":
  22. $image = imagecreatefrompng($file_temporary);
  23. break;
  24. default:
  25. $t = image_type_to_mime_type($image_size[2]);
  26. echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
  27. break;
  28. }
  29.  
  30. $thumb = imagecreatetruecolor($new_width, $new_height);
  31. imagecopyresampled($thumb, $image, 0, 0, 0, 0, $new_width, $new_height, $image_size[0], $image_size[1]);
  32.  
  33. switch($type) {
  34. case "jpg":
  35. case "jpeg":
  36. //header("Content-type: image/jpeg");
  37. imagejpeg($thumb, $file_destination);
  38. break;
  39. case "gif":
  40. //header("Content-type: image/gif");
  41. imagegif($thumb, $file_destination);
  42. break;
  43. case "png":
  44. //header("Content-type: image/png");
  45. imagepng($thumb, $file_destination);
  46. break;
  47. default:
  48. echo "The image type {$type} is not supported, please choose another.";
  49. break;
  50. }
  51.  
  52. imagedestroy($image);
  53. imagedestroy($thumb);
  54.  
  55. return true;
  56. }

以上是关于所有图像到JPG/JPeG转换器和大小调整器的主要内容,如果未能解决你的问题,请参考以下文章

在 PHP 中将 JPG/GIF 图像转换为 PNG?

node js使用graphicsmagick调整文件夹中所有图像的大小

Android-图像原理/绘制原理

怎样把图片转换成 gif,png,jpg,jpeg格式

上载和调整大小

调整大小时Python OpenCV图像打乱[重复]