Image.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $newPath = $infos['dirname'].SLASH.$infos['filename'].'.png';
  29. // compression is a value from 0 (no compression) to 9 (high compression)
  30. imagepng($output, $newPath, 9);
  31. unlink($path);
  32. return $newPath;
  33. }
  34. public static function resize($path,$width,$height,$keepRatio = true,$forceResize = true){
  35. $resource = self::resource($path);
  36. if(!is_resource($resource)) return;
  37. $infos = pathinfo($path);
  38. $oldX = imageSX($resource);
  39. $oldY = imageSY($resource);
  40. // Ne pas resize une image plus petite
  41. if (!$forceResize && $oldX <= $width && $oldY <= $height) {
  42. return;
  43. }
  44. if($oldX > $oldY) {
  45. $thumb_w = $width;
  46. $thumb_h = $oldY*($height/$oldX);
  47. }
  48. if($oldX < $oldY) {
  49. $thumb_w = $oldX*($width/$oldY);
  50. $thumb_h = $height;
  51. }
  52. if($oldX == $oldY) {
  53. $thumb_w = $width;
  54. $thumb_h = $height;
  55. }
  56. if(!$keepRatio){
  57. $thumb_w = $width;
  58. $thumb_h = $height;
  59. }
  60. $dst_resource = ImageCreateTrueColor($thumb_w,$thumb_h);
  61. switch (mb_strtolower($infos['extension'])) {
  62. case 'png':
  63. $resource = ImageCreateFromPNG($path);
  64. imagealphablending($dst_resource, false);
  65. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  66. imagesavealpha($dst_resource, true);
  67. $result = imagepng($dst_resource, $path, 4);
  68. break;
  69. case 'jpg':
  70. case 'jpeg':
  71. imagecopyresampled($dst_resource,$resource,0,0,0,0,$thumb_w,$thumb_h,$oldX,$oldY);
  72. $result = imagejpeg($dst_resource,$path,80);
  73. break;
  74. default:
  75. //On ne peut pas resize les GIF (sans faire une usine à gaz)
  76. //On ne peut resize les BMP que depuis la v7.0 de PHP
  77. break;
  78. }
  79. imagedestroy($dst_resource);
  80. imagedestroy($resource);
  81. }
  82. public static function resource($path){
  83. $infos = pathinfo($path);
  84. switch (mb_strtolower($infos['extension'])){
  85. case 'jfif':
  86. case 'jpg':
  87. case 'jpeg':
  88. return imagecreatefromjpeg($path);
  89. break;
  90. case 'png':
  91. return imagecreatefrompng($path);
  92. break;
  93. case 'gif':
  94. return imagecreatefromgif($path);
  95. break;
  96. // PHP 7 only
  97. /*case 'bmp':
  98. return imagecreatefrombmp($path);
  99. break;*/
  100. default:
  101. return 0;
  102. break;
  103. }
  104. }
  105. /*
  106. Retourne une image selon l'angle définit
  107. Prérequis systeme :
  108. - Installer le paquet imagemagick (apt-get install imagemagick)
  109. nb: Si le pb "convert-im6.q16: not authorized `outfile.pdf' @ error/constitute.c/WriteImage/1037" apparait
  110. editer /etc/ImageMagick-6/policy.xml et changer sur la ligne PDFle droit none à read|write .*/
  111. public static function rotate($image,$destination,$degree){
  112. $command = 'convert ';
  113. $command .= ' "'.$image.'" ';
  114. $command .= ' -rotate '.$degree.' "'.$destination.'"';
  115. $result = shell_exec($command);
  116. return $result;
  117. }
  118. /*
  119. Concatene les images fournies en une seule
  120. Prérequis systeme :
  121. - Installer le paquet imagemagick (apt-get install imagemagick)
  122. */
  123. public static function concatenate($images,$destinationPath){
  124. $command = 'convert -limit width 100KP ';
  125. foreach ($images as $image) {
  126. $command .= ' "'.$image.'" ';
  127. }
  128. $command .= ' -append "'.$destinationPath.'"';
  129. return shell_exec($command);
  130. }
  131. /*
  132. Convertis le tableau de chemin d'images fournis en un PDF
  133. Prérequis systeme :
  134. - Installer le paquet imagemagick (apt-get install imagemagick)
  135. nb: Si le pb "convert-im6.q16: not authorized `outfile.pdf' @ error/constitute.c/WriteImage/1037" apparait
  136. editer /etc/ImageMagick-6/policy.xml et changer sur la ligne PDFle droit none à read|write .*/
  137. public static function toPDF($images,$pdfPath){
  138. $command = 'convert ';
  139. foreach ($images as $image) {
  140. $command .= ' "'.$image.'" ';
  141. }
  142. $command .= ' -quality 100 "'.$pdfPath.'"';
  143. $result = shell_exec ($command);
  144. }
  145. }