Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save agustinprosperi/b6e2f4bf3b8efb782387b659db3c3b8e to your computer and use it in GitHub Desktop.
Save agustinprosperi/b6e2f4bf3b8efb782387b659db3c3b8e to your computer and use it in GitHub Desktop.
Listar archivos recursivamente y reducir tamaño de imágenes con Imagick
<?php
// Listar archivos recursivamente y reducir tamaño de imágenes con Imagick
if( isset($_GET['list_upload_dir']) )
add_action( "init", "list_upload_dir" );
function list_upload_dir(){
$wud = wp_upload_dir();
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $wud['basedir'] ) );
$i = 0;
echo'<meta charset="UTF-8">';
echo'<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">';
echo '<div style="padding:20px 10px;"><table class="table table-striped table-bordered"><thead>';
foreach($iterator as $name=>$file){
// if( $file->isDir() || !is_image($name) ) continue;
if( $file->isDir() ) continue;
$size = $file->getSize()/1024/1024;
if( $size<0.1 ) continue;
$i++;
$result = tt_lower_image_weight($name, $name, 90, 0);
$col = empty($result['error'])? 'green': 'red';
?>
<tr>
<td valign="middle" style="font-size:12px; line-height:1; padding:3px; background:<?php echo $col; ?>; color:white;"><?php echo $i; ?></td>
<td valign="middle" style="font-size:12px; line-height:1; padding:3px; background:<?php echo $col; ?>; color:white;"><?php echo $name; ?></td>
<td valign="middle" style="font-size:12px; line-height:1; padding:3px; background:<?php echo $col; ?>; color:white;"><?php echo number_format($size, 2, '.', '').' MB'; ?></td>
<td valign="middle" style="font-size:12px; line-height:1; padding:3px; background:<?php echo $col; ?>; color:white;"><?php echo empty($result['nsize'])? 'false': number_format($result['nsize'], 2, '.', '').' MB'; ?></td>
<td valign="middle" style="font-size:12px; line-height:1; padding:3px; background:<?php echo $col; ?>; color:white;"><?php echo empty($result['type'])? 'false': $result['type']; ?></td>
</tr>
<?php
// exit();
}
echo '</table></div>';
exit();
}
// reducir tamaño de imágenes con Imagick
function tt_lower_image_weight($from, $to, $q=99, $save=true){
if( !class_exists('Imagick') ) return false;
$ret = array();
$ret['error'] = false;
/* Create the Imagick object */
$im = new Imagick();
try{
if( $im->readImage($from) && $im->valid() ){
$ret['type'] = $im->getImageMimeType();
if( strpos($ret['type'], 'gif')===false ){
$os = $im->getImageLength()/1024/1024;
$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality($q);
$im->stripImage();
$ret['nsize'] = strlen( $im->getImageBlob() )/1024/1024;
if( $save && $os>$ret['nsize'] ){
$ret['saved'] = $im->writeImage( $to );
}
}else{
$ret['error'] = 'is gif';
}
}else{
$ret['error'] = 'not valid';
}
}catch(Exception $e){
$ret['error'] = 'not valid';
}
$im->destroy();
return $ret;
}
// is_image
function is_image($path){
$a = getimagesize($path);
$image_type = $a[2];
// IMAGETYPE_GIF
if( in_array($image_type , array(IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)) ){
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment