Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2. include('m2brimagem.class.php');
  3.  
  4. define('IMAGEPATH', "./original_images");
  5. define('IMAGEPATHMD5', "./temp/images");
  6. define('IMAGEPATHTHUMB', "./temp/thumbs");
  7.  
  8. /****** Cria os diretórios caso não existam ******/
  9. define('DIRS', [IMAGEPATHMD5, IMAGEPATHTHUMB]);
  10. foreach (DIRS as $key => $value) {
  11.     if (!file_exists($value)) {
  12.         mkdir($value, 0777, true);
  13.     }
  14. }
  15.  
  16. /*******************************************************/
  17.  
  18. $media = [];
  19.  
  20. foreach (getImagesArray() as $key => $img) {
  21.  
  22.  
  23.     $img_location = getImageLocation($img);
  24.    
  25.     echo "Comprimindo: " . $img_location . PHP_EOL;
  26.  
  27.  
  28.     if(preg_match('/ /', $img)){
  29.         rename($img_location, IMAGEPATH  . "/" . preg_replace("/ /", "_", $img));
  30.     }
  31.  
  32.     resize($img);
  33.  
  34.     $new_image = getImageLocationMD5($img, true);
  35.  
  36.     compressImage($new_image, $new_image, 25);
  37.  
  38.     resizeToSearch($img);
  39.  
  40.     $media[$key]['ordem'] = 1;
  41.     $media[$key]['url'] = "https://imagens.loopimoveis.com/" . getImageMD5($img) . getImgExtension($img);
  42.     $media[$key]['legenda'] = "";
  43.    
  44. }
  45.  
  46. echo PHP_EOL;
  47. echo PHP_EOL;
  48. echo "JSON:";
  49. echo PHP_EOL;
  50. echo PHP_EOL;
  51. echo json_encode($media) . PHP_EOL;
  52. echo PHP_EOL;
  53.  
  54. /*******************************************************/
  55.  
  56. function getImgExtension($img){
  57.     return "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
  58. }
  59.  
  60. function getImagesArray(){
  61.     $dir_files = scandir(IMAGEPATH);
  62.     unset($dir_files[0]);
  63.     unset($dir_files[1]);
  64.     return array_values($dir_files);;
  65. }
  66.  
  67. function getImageLocation($img){
  68.     return IMAGEPATH . "/" . $img;
  69. }
  70.  
  71. function resize($img){
  72.     $image_location = getImageLocation($img);
  73.  
  74.     $width = getimagesize($image_location)[0];
  75.  
  76.     $oImg = new m2brimagem($image_location);
  77.  
  78.     $valida = $oImg->valida();
  79.  
  80.     if ($valida == 'OK') {
  81.        
  82.         if ($width > 1920) {
  83.             $oImg->redimensiona(1920, '','crop');
  84.         }
  85.  
  86.         $oImg->grava(getImageLocationMD5($img, true));
  87.     } else {
  88.         die($valida);
  89.     }
  90. }
  91.  
  92. function resizeToSearch($img){
  93.     $image_location = getImageLocationMD5($img, true);
  94.  
  95.     echo "Criando thumb em: " . getImageLocationThumb($img) . PHP_EOL;
  96.  
  97.     $width = getimagesize($image_location)[0];
  98.  
  99.     $oImg = new m2brimagem($image_location);
  100.  
  101.     $valida = $oImg->valida();
  102.  
  103.     if ($valida == 'OK') {
  104.        
  105.         if ($width > 360) {
  106.             $oImg->redimensiona(360, '','crop');
  107.         }
  108.  
  109.         $oImg->grava(getImageLocationThumb($img));
  110.  
  111.         compressImage(getImageLocationThumb($img), getImageLocationThumb($img), 55);
  112.  
  113.     } else {
  114.         die($valida);
  115.     }
  116. }
  117.  
  118. function getImageLocationThumb($img){
  119.     $ext = "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
  120.     return IMAGEPATHTHUMB . "/" . getImageMD5($img) . "_thumb" . $ext;
  121. }
  122.  
  123. function getImageLocationMD5($img, $extension = false){
  124.     $ext = "";
  125.     if ($extension) {
  126.         $ext = "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
  127.     }
  128.  
  129.     $md5 = getImageMD5($img);
  130.  
  131.     return IMAGEPATHMD5 . "/" . $md5 . $ext;
  132. }
  133.  
  134. function getImageMD5($img){
  135.     $img = file_get_contents(getImageLocation($img));
  136.     return md5($img);
  137. }
  138.  
  139.  
  140. function compressImage($source_path, $destination_path, $quality) {
  141.     $info = getimagesize($source_path);
  142.  
  143.     if ($info['mime'] == 'image/jpeg') {
  144.         $image = imagecreatefromjpeg($source_path);
  145.     } elseif ($info['mime'] == 'image/png') {
  146.         $image = imagecreatefrompng($source_path);
  147.     }
  148.  
  149.     imagejpeg($image, $destination_path, $quality);
  150.  
  151.     return $destination_path;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement