Image Compression on RSS Ticker

Image Compression on RSS Ticker

I’m noticing that some images come in a very large size without any compression.

What if you implemented the compression of images?

This will greatly reduce bandwidth and storage space.

In wordpress there is a plugin called “imsanity” that does this, the user defines the settings and all the images sent that meet the parameters are automatically compressed and resized.

They could also allow you to set a limit for displaying news on the rss ticker.

So that the old ones with more than 1 or 2 months were erased.

Or a maximum limit of news, 10, 20, 30, where whenever a new one was added an old one was deleted.

The way the media folder is growing infinitely. it does not make sense to display this.

Hello.
I created a code that reduces the size of the ticker widget images.

Just create a php file in the root folder of the hosting “out of the xibo folder”, configure the parameters according to your need and run the file.

However, this file must be executed frequently because the widget is always downloading new images.

How about adapting my code code to compress the images of the widget as soon as they were downloaded?

<?php
ini_set('memory_limit', '-1'); /*resolvendo o erro de falta de memoria*/
set_time_limit(0);
/*error_reporting(0);  */

/* Definindo a pasta das imagens */
$dir_img = "xibo/web/midia/";
/* Definindo a pasta midia/temp */
$dir_temp = "xibo/web/midia/temp/";
/* Handler do diretório */
$dh = opendir($dir_img); 
$total = 0;

/* Função compactar */
function compressImage($source_path, $destination_path, $quality, $copy_path, $file_name) {
/* Obtendo informações da imagem */
$size = getimagesize($source_path); 
/* Se a largura for maior que 600 será reduzida para 800 */
if ($size[0] > 600){
   $new_width = "600";
/* Se for menor a largura original será mantida */
} else ($new_width = $size[0]);
/* Obtendo a altura proporcional a largura */
$new_height = ( int )(( $new_width/$size[0] )*$size[1] );
/* Criando a imagem com cores reais e dimensões escolhidas */
$img_true_color = ImageCreateTrueColor( $new_width, $new_height );
/* Criando imagem no formato correto */
if ($size['mime'] == 'image/jpeg') {
   $image = imagecreatefromjpeg($source_path);
} elseif ($size['mime'] == 'image/png') {
   $image = imagecreatefrompng($source_path);
} elseif ($size['mime'] == 'image/gif') {
   $image = imagecreatefromgif($source_path);
} elseif ($size['mime'] == 'image/bmp') {
   $image = imagecreatefrombmp($source_path);
}
/* Criando a imagem redimensionada */
ImageCopyResampled( $img_true_color, $image, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1] );
/* Salva a imagem em jpg com as configurações definidas na chamada */
imagejpeg($img_true_color, $destination_path, $quality);

/* Copiando a imagem compactada para a pasta temp */
if ( @copy( $source_path, $copy_path ) ) {
/*    echo "<br>Arquivo copiado.<br>"; */
} else {
/*    echo "<br>Arquivo nao copiado.<br>"; */
}

/* Obtendo novo tamanho do arquivo */
$novo_tamanho = filesize($copy_path);
echo " | novo tamanho: ".$novo_tamanho;
/* Obtendo novo MD5 do arquivo */
$novo_md5 = md5_file($source_path);
echo " | novo md5: ".$novo_md5;

/* Inserindo novo tamanho e MD5 no banco de dados */
/* Create connection */
$conn = mysqli_connect("localhost", "mucur615_dtvxb", "8~Nm?i%r+TW_", "mucur615_dtvxb");
/* Check connection */
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
/* Atualizando BD */
$sql = "UPDATE media SET FileSize='$novo_tamanho', MD5='$novo_md5' WHERE name='$file_name'";
if (mysqli_query($conn, $sql)) {
/*    echo "<br>Record updated successfully"; */
} else {
/*    echo "<br>Error updating record: " . mysqli_error($conn); */
}
mysqli_close($conn);

return $destination_path;
/* Limpa da memoria a imagem criada temporáriamente */
ImageDestroy( $img_true_color );
} /* Fim da função */

/* Executa enquanto existirem arquivos para listar no diretório */
while (false !== ($filename = readdir($dh))) {
   /*echo "<br>filename: ".substr($filename,0,6)." | dir_img: ".$dir_img." | dh: ".$dh." | ";*/
   /* Verifica se o arquivo inicia com "ticker" */
   if (substr($filename,0,6) == "ticker") {
      /* Verifica tamanho e largura */
      $largura = getimagesize($dir_img.$filename);
      /*echo "<br>------- filename: ".$filename." - filesize: ".filesize($dir_img.$filename)." | largura: ".$largura[0]." | ";*/
      if ((filesize($dir_img.$filename) > 1000000) || ($largura[0] > 600)  /**/ ) {
         $total ++;
         echo "<br> | ".$total." - ".date('H:i:s')." | ".$filename." | tamanho original ".filesize($dir_img.$filename)." | largura ".$largura[0];
         /* Chama a função para cada arquivo que inicia com "ticker" */ 
        $img = compressImage($dir_img.$filename, $dir_img.$filename, 50, $dir_temp.$filename, $filename);
      }  
   }
}
?>
1 Like

Thanks for consider my sugestion.

This topic was automatically closed after 68 days. New replies are no longer allowed.