Jump to content

utiliser imagemagick pour générer les images au lieu de GD


xpoitau

Recommended Posts

Voici ma petite bidouille pour remplacer GD par imagemagick dans images.inc.php

remplacer la fonction
----

   function imageResize($sourceFile, $destFile, $destWidth = NULL, $destHeight = NULL, $fileType = 'jpg')
{
   list($sourceWidth, $sourceHeight, $type, $attr) = getimagesize($sourceFile);
   if (!$sourceWidth)
       return false;
   if ($destWidth == NULL) $destWidth = $sourceWidth;
   if ($destHeight == NULL) $destHeight = $sourceHeight;

   $sourceImage = createSrcImage($type, $sourceFile);

   $widthDiff = $destWidth / $sourceWidth;
   $heightDiff = $destHeight / $sourceHeight;

   if ($widthDiff > 1 AND $heightDiff > 1)
   {
       $nextWidth = $sourceWidth;
       $nextHeight = $sourceHeight;
   }
   else
   {
       if (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 2 OR (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 0 AND $widthDiff > $heightDiff))
       {
           $nextHeight = $destHeight;
           $nextWidth = intval(($sourceWidth * $nextHeight) / $sourceHeight);
           $destWidth = (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 0 ? $destWidth : $nextWidth);
       }
       else
       {
           $nextWidth = $destWidth;
           $nextHeight = intval($sourceHeight * $destWidth / $sourceWidth);
           $destHeight = (intval(Configuration::get('PS_IMAGE_GENERATION_METHOD')) == 0 ? $destHeight : $nextHeight);
       }
   }

   $borderWidth = intval(($destWidth - $nextWidth) / 2);
   $borderHeight = intval(($destHeight - $nextHeight) / 2);

   /*
   $destImage = imagecreatetruecolor($destWidth, $destHeight);

   $white = imagecolorallocate($destImage, 255, 255, 255);
   imagefill($destImage, 0, 0, $white);

   imagecopyresampled($destImage, $sourceImage, $borderWidth, $borderHeight, 0, 0, $nextWidth, $nextHeight, $sourceWidth, $sourceHeight);
   imagecolortransparent($destImage, $white);
   */

   // ajout pour imagemagick
   $bordure = $borderWidth+$borderHeight;
   $cmd = "convert -size ".$sourceWidth."x".$sourceHeight." ".$sourceFile." -resize ".$nextWidth."x".$nextHeight." -bordercolor white -border ".$bordure."x".$bordure." -gravity center -crop ".$destWidth."x".$destHeight."+0+0 ".$destFile;
   exec($cmd);

   return (returnDestImage($fileType, $destImage, $destFile));
}



et aussi la fonction :

function returnDestImage($type, $ressource, $filename)
{
   /*
   $flag = false;
   switch ($type)
   {
       case 'gif':
           $flag = imagegif($ressource, $filename);
           break;
       case 'png':
           $flag = imagepng($ressource, $filename, 7);
           break;
       case 'jpeg':
       default:
           $flag = imagejpeg($ressource, $filename, 90);
           break;
   }
   imagedestroy($ressource);
   return $flag;
   */
   if (file_exists("$filename")) {
       return true;
   } else {
       return false;
   }

}



bien sûr, il faut avoir imagemagick installé et la possibilité d'exécuter des commandes en php.

Xavier
axome.com

images.inc.php

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...