/** * 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); } }
/** * 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); } }
function resizeMobile2($src_path, $dest_path, $d_width, $d_height) { $mk = NewMagickWand(); if (!MagickPingImage($mk, $src_path)) { echo "magick wand - no image \n"; $format = sprintf("convert %s -resize %dx%d -colors 256 -quality 90 -depth 8 %s", $src_path, $destWidth, $destHeight, $dest_path); $buffer = ""; exec($format, $buffer); return false; } // Now we need to clear out the data that MagickPingImage() put there ClearMagickWand($mk); if (MagickReadImage($mk, $src_path)) { list($srcWidth, $srcHeight, $destWidth, $destHeight) = getRate($src_path, $d_width, $d_height); //소스 이미지를 읽어서 $mk = MagickTransformImage($mk, NULL, $destWidth . "x" . $destHeight); MagickSetImageCompressionQuality($mk, 90); MagickSetImageDepth($mk, 8); //MagickSetImageIndex($mk, 256); MagickProfileImage($mk, "*", ""); MagickQuantizeImage($mk, 256, MW_RGBColorspace, 0, true, false); //$chk = MagickResizeImage($mk, $destWidth, $destHeight); //echo "$src_path , $dest_path, $destWidth, $destHeight \n"; // 이미지를 리사이징해라. 가로 $w 세로 $h //MagickResizeImage() 이라는 함수도 있는데 위의 것이 더 범용적입니다. if ($mk == null) { //echo "this is convert"; $format = sprintf("convert %s -resize %dx%d -colors 256 -quality 90 -depth 8 %s", $src_path, $destWidth, $destHeight, $dest_path); $buffer = ""; exec($format, $buffer); //echo "object is null \n"; return true; } MagickWriteImage($mk, $dest_path); // 새로운 이미지를 만들어라~ ClearMagickWand($mk); } else { echo "magick wand - read fail \n"; return false; } return true; }