jueves, 4 de mayo de 2017

PHP - Image Resize


method to reduce the image size without losing quality


<?php

$thumb_width = 700;
$thumb_height = 90;

$extes = array( 'jpg', 'JPG' );
$ins = scandir('./');

$cmpt = 0;
foreach( $ins as $f ) {
    if( $f === '.'  || $f === '..' ) { continue; }
    if( is_file($f) ) {
        
        $parts = pathinfo($f);
        if( in_array(strtolower($parts['extension']), $extes ) ) {

            $cmpt++;
            echo $f."<br />";
            
            $srcImg = imagecreatefromjpeg($f);
            $ampla = imagesx($srcImg);
            $alt = imagesy($srcImg);
            $ratio = $ampla / $alt;
            
            $nouAmpla = $thumb_width;
            $nouAlt = floor($nouAmpla / $ratio);
            
            $thumb_img = imagecreatetruecolor($nouAmpla, $nouAlt);
            imagecopyresampled($thumb_img, $srcImg, 0, 0, 0, 0, $nouAmpla, $nouAlt, $ampla, $alt);
            imagejpeg($thumb_img, "../".$f);
        }
    }
}
echo "<br />Image Count = ".$cmpt;
?>

No hay comentarios:

Publicar un comentario