Image.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class Image{
  3. public static function toJpg($path){
  4. $infos = pathinfo($path);
  5. if($infos['extension']=='jpg' || $infos['extension']=='jpeg') return $path;
  6. //Make image with white background instead of black when converting png to jpg
  7. $input = self::resource($path);
  8. list($width, $height) = getimagesize($path);
  9. $output = imagecreatetruecolor($width, $height);
  10. $white = imagecolorallocate($output, 255, 255, 255);
  11. imagefilledrectangle($output, 0, 0, $width, $height, $white);
  12. imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
  13. $newPath = $infos['dirname'].SLASH.$infos['filename'].'.jpg';
  14. // quality is a value from 0 (worst) to 100 (best)
  15. imagejpeg($output, $newPath, 100);
  16. unlink($path);
  17. return $newPath;
  18. }
  19. public static function toPng($path){
  20. $infos = pathinfo($path);
  21. if($infos['extension']=='png') return;
  22. $input = self::resource($path);
  23. list($width, $height) = getimagesize($path);
  24. $output = imagecreatetruecolor($width, $height);
  25. $white = imagecolorallocate($output, 255, 255, 255);
  26. imagefilledrectangle($output, 0, 0, $width, $height, $white);
  27. imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
  28. // compression is a value from 0 (no compression) to 9 (high compression)
  29. imagepng($output, $infos['dirname'].SLASH.$infos['filename'].'.png', 9);
  30. unlink($path);
  31. }
  32. public static function resize($path,$width,$height,$keepRatio = true,$forceResize = true){
  33. $resource = self::resource($path);
  34. $infos = pathinfo($path);
  35. $oldX = imageSX($resource);
  36. $oldY = imageSY($resource);
  37. // Ne pas resize une image plus petite
  38. if (!$forceResize && $oldX <= $width && $oldY <= $height) {
  39. return;
  40. }
  41. if($oldX > $oldY) {
  42. $thumb_w = $width;
  43. $thumb_h = $oldY*($height/$oldX);
  44. }
  45. if($oldX < $oldY) {
  46. $thumb_w = $oldX*($width/$oldY);
  47. $thumb_h = $height;
  48. }
  49. if($oldX == $oldY) {
  50. $thumb_w = $width;
  51. $thumb_h = $height;
  52. }
  53. if(!$keepRatio){
  54. $thumb_w = $width;
  55. $thumb_h = $height;
  56. }
  57. $dst_resource = ImageCreateTrueColor($thumb_w,$thumb_h);
  58. switch (mb_strtolower($infos['extension'])) {
  59. case 'png':
  60. $resource = ImageCreateFromPNG($path);
  61. imagealphablending($dst_resource, false);
  62. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  63. imagesavealpha($dst_resource, true);
  64. $result = imagepng($dst_resource, $path, 4);
  65. break;
  66. case 'jpg':
  67. case 'jpeg':
  68. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  69. $result = imagejpeg($dst_resource,$path,80);
  70. break;
  71. default:
  72. //On ne peut pas resize les GIF (sans faire une usine à gaz)
  73. //On ne peut resize les BMP que depuis la v7.0 de PHP
  74. break;
  75. }
  76. imagedestroy($dst_resource);
  77. imagedestroy($resource);
  78. }
  79. public static function resource($path){
  80. $infos = pathinfo($path);
  81. switch (mb_strtolower($infos['extension'])){
  82. case 'jpg':
  83. case 'jpeg':
  84. return imagecreatefromjpeg($path);
  85. break;
  86. case 'png':
  87. return imagecreatefrompng($path);
  88. break;
  89. case 'gif':
  90. return imagecreatefromgif($path);
  91. break;
  92. default:
  93. return 0;
  94. break;
  95. }
  96. }
  97. }
  98. ?>