Localisation: 1&1 FAQ ->Scripts / Programmation->PHP->Exemples de Scripts->Article #1

Hébergement Linux Cet article n'est applicable qu'aux hébergements Linux.

Compresser une image à l’aide de GD
Voici un petit exemple de script vous permettant la compression d’une image (.jpg) à l’aide de GD présente chez 1&1 Internet.

Fichier 1: reduction.php
<?php
  header("Content-type: image/jpeg");
    
  $file= "image.jpg";
  $path= "image";
  $filepath = $path."/".$file;
  if(file_exists("$filepath")) {
    $h_img= imagecreatefromjpeg("$path/$file");
    $width= imagesx($h_img);
    $height= imagesy($h_img);

    $thb_x= 100;
    $thb_y= 100;
    $h_thb= imagecreatetruecolor($thb_x, $thb_y);

    imagecopyresized(
      $h_thb, 
      $h_img,
      0,
      0,
      0,
      0,
      $thb_x,
      $thb_y,
      $width,
      $height
    );
    imagejpeg($h_thb, "$file");
    imagedestroy($h_thb);
    imagedestroy($h_img);
  }
  readfile("$file");
?>

Astuce:
Une documentation supplémentaire est disponible sur le site de php.net
Consultez PHP.net pour la manipulation d'images.