Skip to content

Instantly share code, notes, and snippets.

@pereirawe
Created October 30, 2019 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pereirawe/8cb8e77d5d9972b18fde3a569a2a9d67 to your computer and use it in GitHub Desktop.
Save pereirawe/8cb8e77d5d9972b18fde3a569a2a9d67 to your computer and use it in GitHub Desktop.
Create a folder with miniatures from another folder
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Convertir imagen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css">
<script src="main.js"></script>
</head>
<body>
<?php
function make_thumb_re($src, $dest, $desired_width) {
$source_image = imagecreatefromjpeg($src);
// $source_image = imagecreatefrompng($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagejpeg($virtual_image, $dest);
}
function cron_reimg( $path , $new_path , $max_size){
$directorio = opendir( $path );
$directorio_dest = opendir( $new_path );
$limit = 0;
while ($archivo = readdir($directorio)) {
if (!is_dir($archivo)) {
$actual_img_width = getimagesize($path . $archivo)[0] ;
$actual_img_height = getimagesize($path . $archivo)[1];
$dest_img_width = getimagesize($new_path . $archivo)[0] ;
$dest_img_height = getimagesize($new_path . $archivo)[1];
$imageFileType = strtolower(pathinfo($archivo,PATHINFO_EXTENSION));
if ( !isset($dest_img_width) ) {
if ($imageFileType == "jpg" || $imageFileType == "jpeg"){
if ( $actual_img_width > $max_size || $actual_img_height > $max_size ){
$old_size = getimagesize($path . $archivo);
$src= $path . $archivo;
$dest = $new_path . $archivo;
$limit = $limit +1;
echo "<h4>Proceso ". $limit ."</h4>";
//echo "<p><small>Reducción: ".$reduccion."</small></p>";
echo "Imagen original:" .$src . "<br>";
echo "Imagen destino:" .$dest . "<hr>";
$desired_width= $max_size;
make_thumb_re($src, $dest , $desired_width);
$new_size = getimagesize($new_path . $archivo);
$reduccion = ($new_size[0] / $old_size[0]) * 100;
if ($limit == 50) {
echo "<script>
function myreload() {
/* alert('Hello'); */
location.reload();
};
setTimeout( myreload,5000 );
</script>";
//die();
}
};
};
};
// CONVERTIR .png en .jpg
if($imageFileType == "png"){
$filePath = $path . $archivo;
$image = imagecreatefrompng($filePath);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagedestroy($image);
$quality = 90; // 0 = worst / smaller file, 100 = better / bigger file
$filePath = substr($filePath, 0 ,-3)."jpg";
imagejpeg($bg, $filePath , $quality);
imagedestroy($bg);
}
};
};
};
cron_reimg("./img/productos/" , "./img/productos_100/" , 100 );
//cron_reimg("./rs_sliders/" , "./rs_sliders_750/" , 750 );
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment