Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include('m2brimagem.class.php');
- define('IMAGEPATH', "./original_images");
- define('IMAGEPATHMD5', "./temp/images");
- define('IMAGEPATHTHUMB', "./temp/thumbs");
- /****** Cria os diretórios caso não existam ******/
- define('DIRS', [IMAGEPATHMD5, IMAGEPATHTHUMB]);
- foreach (DIRS as $key => $value) {
- if (!file_exists($value)) {
- mkdir($value, 0777, true);
- }
- }
- /*******************************************************/
- $media = [];
- foreach (getImagesArray() as $key => $img) {
- $img_location = getImageLocation($img);
- echo "Comprimindo: " . $img_location . PHP_EOL;
- if(preg_match('/ /', $img)){
- rename($img_location, IMAGEPATH . "/" . preg_replace("/ /", "_", $img));
- }
- resize($img);
- $new_image = getImageLocationMD5($img, true);
- compressImage($new_image, $new_image, 25);
- resizeToSearch($img);
- $media[$key]['ordem'] = 1;
- $media[$key]['url'] = "https://imagens.loopimoveis.com/" . getImageMD5($img) . getImgExtension($img);
- $media[$key]['legenda'] = "";
- }
- echo PHP_EOL;
- echo PHP_EOL;
- echo "JSON:";
- echo PHP_EOL;
- echo PHP_EOL;
- echo json_encode($media) . PHP_EOL;
- echo PHP_EOL;
- /*******************************************************/
- function getImgExtension($img){
- return "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
- }
- function getImagesArray(){
- $dir_files = scandir(IMAGEPATH);
- unset($dir_files[0]);
- unset($dir_files[1]);
- return array_values($dir_files);;
- }
- function getImageLocation($img){
- return IMAGEPATH . "/" . $img;
- }
- function resize($img){
- $image_location = getImageLocation($img);
- $width = getimagesize($image_location)[0];
- $oImg = new m2brimagem($image_location);
- $valida = $oImg->valida();
- if ($valida == 'OK') {
- if ($width > 1920) {
- $oImg->redimensiona(1920, '','crop');
- }
- $oImg->grava(getImageLocationMD5($img, true));
- } else {
- die($valida);
- }
- }
- function resizeToSearch($img){
- $image_location = getImageLocationMD5($img, true);
- echo "Criando thumb em: " . getImageLocationThumb($img) . PHP_EOL;
- $width = getimagesize($image_location)[0];
- $oImg = new m2brimagem($image_location);
- $valida = $oImg->valida();
- if ($valida == 'OK') {
- if ($width > 360) {
- $oImg->redimensiona(360, '','crop');
- }
- $oImg->grava(getImageLocationThumb($img));
- compressImage(getImageLocationThumb($img), getImageLocationThumb($img), 55);
- } else {
- die($valida);
- }
- }
- function getImageLocationThumb($img){
- $ext = "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
- return IMAGEPATHTHUMB . "/" . getImageMD5($img) . "_thumb" . $ext;
- }
- function getImageLocationMD5($img, $extension = false){
- $ext = "";
- if ($extension) {
- $ext = "." . pathinfo(getImageLocation($img), PATHINFO_EXTENSION);
- }
- $md5 = getImageMD5($img);
- return IMAGEPATHMD5 . "/" . $md5 . $ext;
- }
- function getImageMD5($img){
- $img = file_get_contents(getImageLocation($img));
- return md5($img);
- }
- function compressImage($source_path, $destination_path, $quality) {
- $info = getimagesize($source_path);
- if ($info['mime'] == 'image/jpeg') {
- $image = imagecreatefromjpeg($source_path);
- } elseif ($info['mime'] == 'image/png') {
- $image = imagecreatefrompng($source_path);
- }
- imagejpeg($image, $destination_path, $quality);
- return $destination_path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement