Image.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. class Image{
  3. public static function toJpg($path){
  4. $infos = pathinfo($path);
  5. if($infos['extension']=='jpg' || $infos['extension']=='jpeg') return;
  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. // quality is a value from 0 (worst) to 100 (best)
  14. imagejpeg($output, $infos['dirname'].SLASH.$infos['filename'].'.jpg', 100);
  15. unlink($path);
  16. }
  17. public static function toPng($path){
  18. $infos = pathinfo($path);
  19. if($infos['extension']=='png') return;
  20. $input = self::resource($path);
  21. list($width, $height) = getimagesize($path);
  22. $output = imagecreatetruecolor($width, $height);
  23. $white = imagecolorallocate($output, 255, 255, 255);
  24. imagefilledrectangle($output, 0, 0, $width, $height, $white);
  25. imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
  26. // compression is a value from 0 (no compression) to 9 (high compression)
  27. imagepng($output, $infos['dirname'].SLASH.$infos['filename'].'.png', 9);
  28. unlink($path);
  29. }
  30. public static function resize($path,$width,$height,$keepRatio = true){
  31. $resource = self::resource($path);
  32. $infos = pathinfo($path);
  33. $oldX = imageSX($resource);
  34. $oldY = imageSY($resource);
  35. if($oldX > $oldY)
  36. {
  37. $thumb_w = $width;
  38. $thumb_h = $oldY*($height/$oldX);
  39. }
  40. if($oldX < $oldY)
  41. {
  42. $thumb_w = $oldX*($width/$oldY);
  43. $thumb_h = $height;
  44. }
  45. if($oldX == $oldY)
  46. {
  47. $thumb_w = $width;
  48. $thumb_h = $height;
  49. }
  50. if(!$keepRatio){
  51. $thumb_w = $width;
  52. $thumb_h = $height;
  53. }
  54. $dst_resource = ImageCreateTrueColor($thumb_w,$thumb_h);
  55. if($infos['extension']=='png') {
  56. $resource = ImageCreateFromPNG($path);
  57. imagealphablending($dst_resource, false);
  58. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  59. imagesavealpha($dst_resource, true);
  60. $result = imagepng($dst_resource, $path, 6);
  61. }
  62. if($infos['extension']=='jpg' || $infos['extension']=='jpeg') {
  63. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  64. $result = imagejpeg($dst_resource,$path,80);
  65. }
  66. imagedestroy($dst_resource);
  67. imagedestroy($resource);
  68. }
  69. public static function resource($path){
  70. $infos = pathinfo($path);
  71. switch (mb_strtolower($infos['extension'])){
  72. case 'jpg':
  73. case 'jpeg':
  74. return imagecreatefromjpeg($path);
  75. break;
  76. case 'png':
  77. return imagecreatefrompng($path);
  78. break;
  79. case 'gif':
  80. return imagecreatefromgif($path);
  81. break;
  82. case 'bmp':
  83. return imagecreatefrombmp($path);
  84. default:
  85. return 0;
  86. break;
  87. }
  88. }
  89. }
  90. ?>