/** * Resize an image to a specific width/height * * @param int $maxWidth maximum image Width (px) * @param int $maxHeight maximum image Height (px) * @param string $origFile current images folder path (must have trailing end slash) * @param string $destFile destination folder path for resized image (must have trailing end slash) * @param int $quality Percentage image save quality 100 = no compression, 0 = max compression * * @return object image */ public function resize($maxWidth, $maxHeight, $origFile, $destFile, $quality = 100) { $ext = $this->getImgType($origFile); if (!$ext) { // False so not an image type so cant resize // $$$ hugh - testing making thumbs for PDF's, so need a little tweak here $origInfo = pathinfo($origFile); if (JString::strtolower($origInfo['extension']) != 'pdf') { return; } } ini_set('display_errors', true); // See if the imagick image lib is installed if (class_exists('Imagick')) { /* $$$ hugh - having a go at handling PDF thumbnails, which should work as long as the server * has ghostscript (GS) installed. Don't have a generic test for GS being available, so * it'll just fail if no GS. */ $origInfo = pathinfo($origFile); if (JString::strtolower($origInfo['extension']) == 'pdf') { $pdfThumbType = 'png'; // OK, it's a PDF, so first we need to add the page number we want to the source filename $pdfFile = $origFile . '[0]'; if (is_callable('exec')) { $destFile = str_replace('.pdf', '.png', $destFile); // Output File $convert = "convert " . $pdfFile . " -colorspace RGB -resize " . $maxWidth . " " . $destFile; // Command creating exec($convert); // Execution of complete command. } else { // Now just load it, set format, resize, save and garbage collect. // Hopefully IM will call the right delegate (ghostscript) to load the PDF. $im = new Imagick($pdfFile); $im->setImageFormat($pdfThumbType); $im->thumbnailImage($maxWidth, $maxHeight, true); $im->writeImage($destFile); // as destroy() is deprecated $im->clear(); } } else { $im = new Imagick(); /* Read the image file */ $im->readImage($origFile); /* Thumbnail the image ( width 100, preserve dimensions ) */ $im->thumbnailImage($maxWidth, $maxHeight, true); /* Write the thumbnail to disk */ $im->writeImage($destFile); /* Free resources associated to the Imagick object */ $im->destroy(); } $this->thumbPath = $destFile; } else { $resource = NewMagickWand(); if (!MagickReadImage($resource, $origFile)) { echo "ERROR!"; print_r(MagickGetException($resource)); } $resource = MagickTransformImage($resource, '0x0', $maxWidth . 'x' . $maxWidth); $this->thumbPath = $destFile; MagickWriteImage($resource, $destFile); } }
/** * resize an image to a specific width/height using imagemagick * you cant set the quality of the resized image * @param int maximum image Width (px) * @param int maximum image Height (px) * @param string full path of image to resize * @param string full file path to save resized image to * @return string output from image magick command */ function resize($maxWidth, $maxHeight, $origFile, $destFile) { $ext = $this->GetImgType($origFile); if (!$ext) { //false so not an image type so cant resize // $$$ hugh - testing making thumbs for PDF's, so need a little tweak here $originfo = pathinfo($origFile); if (strtolower($originfo['extension']) != 'pdf') { return; } } ini_set('display_errors', true); //see if the imagick image lib is installed if (class_exists('Imagick')) { // $$$ hugh - having a go at handling PDF thumbnails, which should work as long as the server // has ghostscript (GS) installed. Don't have a generic test for GS being available, so // it'll just fail if no GS. $originfo = pathinfo($origFile); if (strtolower($originfo['extension']) == 'pdf') { $pdf_thumb_type = 'png'; // could use jpg or whatever // OK, it's a PDF, so first we need to add the page number we want to the source filename $pdf_file = $origFile . '[0]'; // Now check to see if the destination filename needs changing - existing code will probably // just have used the sourcefile extension for the thumb file. $destinfo = pathinfo($destFile); if (strtolower($destinfo['extension']) == 'pdf') { // rebuild $destFile with valid image extension // NOTE - changed $destFile arg to pass by reference OOOPS can't do that! // $$$ rob 04/08/2011 wont work in php 5.1 //$destFile = $destinfo['dirname'] . DS . $destinfo['filename'] . '.' . $pdf_thumb_type; $thumb_file = JFile::stripExt($destFile) . '.' . $pdf_thumb_type; } // Now just load it, set format, resize, save and garbage collect. // Hopefully IM will call the right delagate (ghostscript) to load the PDF. $im = new Imagick($pdf_file); $im->setImageFormat($pdf_thumb_type); $im->thumbnailImage($maxWidth, $maxHeight, true); $im->writeImage($destFile); $im->destroy(); } else { $im = new Imagick(); /* Read the image file */ $im->readImage($origFile); /* Thumbnail the image ( width 100, preserve dimensions ) */ $im->thumbnailImage($maxWidth, $maxHeight, true); /* Write the thumbail to disk */ $im->writeImage($destFile); /* Free resources associated to the Imagick object */ $im->destroy(); } $this->_thumbPath = $destFile; } else { $resource = NewMagickWand(); if (!MagickReadImage($resource, $origFile)) { echo "ERROR!"; print_r(MagickGetException($resource)); } $resource = MagickTransformImage($resource, '0x0', $maxWidth . 'x' . $maxWidth); $this->_thumbPath = $destFile; MagickWriteImage($resource, $destFile); } }