/** * 缩略(放大)图片 * @param int $thumbWidth 缩放的最大宽度 * @param int $thumbHeight 缩放的最大高度 * @param bool $magnify 是否允许放大图片 * @return bool 成功返回true,失败返回false */ public function resize($thumbWidth = 0, $thumbHeight = 0, $magnify = true) { $width = $this->getWidth(); $height = $this->getHeight(); do { //定宽缩放 if ($thumbHeight === 0) { $thumbHeight = $thumbWidth / $width * $height; break; } //定高缩放 if ($thumbWidth === 0) { $thumbWidth = $thumbHeight / $height * $width; break; } //等比例缩放 if ($width / $thumbWidth > $height / $thumbHeight) { $ratio = $thumbWidth / $width; } else { $ratio = $thumbHeight / $height; } $thumbWidth = $width * $ratio; $thumbHeight = $height * $ratio; } while (0); //由于图片是等比率的,所以只需判断一条边 if (!$magnify && $thumbWidth >= $width) { return true; } MagickSetImageCompressionQuality(self::$resource, 90); //1-100值越大,越清晰 return MagickScaleImage(self::$resource, $thumbWidth, $thumbHeight); }
function makeThumbnailtoFile($destFile) { $returnVal = false; if (!$this->isWorking()) { return false; } $image = NewMagickWand(); MagickReadImage($image, $this->sourceFile); MagickSetImageCompressionQuality($image, $this->thumbQuality); MagickThumbnailImage($image, $this->thumbWidth, $this->thumbHeight); $returnVal = MagickWriteImage($image, $destFile); unset($image); return $returnVal; }
/** * liberty_magickwand_resize_image * * @param array $pFileHash * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function liberty_magickwand_resize_image(&$pFileHash) { global $gBitSystem; // static var here is crucial static $rgbConverts = array(); $magickWand = NewMagickWand(); $pFileHash['error'] = NULL; $ret = NULL; if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file']) && filesize($pFileHash['source_file'])) { if ($error = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand)) { // $pFileHash['error'] = $error; $destFile = liberty_process_generic($pFileHash, FALSE); } else { if (MagickGetImageColorspace($magickWand) == MW_CMYKColorspace) { // These two lines are a hack needed for version of Ghostscript less that 8.60 MagickRemoveImageProfile($magickWand, "ICC"); MagickSetImageProfile($magickWand, 'ICC', file_get_contents(UTIL_PKG_PATH . 'icc/USWebCoatedSWOP.icc')); MagickProfileImage($magickWand, 'ICC', file_get_contents(UTIL_PKG_PATH . 'icc/srgb.icm')); MagickSetImageColorspace($magickWand, MW_RGBColorspace); $pFileHash['colorspace_conversion'] = TRUE; } MagickSetImageCompressionQuality($magickWand, $gBitSystem->getConfig('liberty_thumbnail_quality', 85)); $iwidth = round(MagickGetImageWidth($magickWand)); $iheight = round(MagickGetImageHeight($magickWand)); // this does not seem to be needed. magickwand will work out what to do by using the destination file extension //MagickSetImageFormat( $magickWand, $format ); if (empty($pFileHash['max_width']) && empty($pFileHash['max_height']) || !empty($pFileHash['max_width']) && $pFileHash['max_width'] == MAX_THUMBNAIL_DIMENSION || !empty($pFileHash['max_height']) && $pFileHash['max_height'] == MAX_THUMBNAIL_DIMENSION) { $pFileHash['max_width'] = $iwidth; $pFileHash['max_height'] = $iheight; } elseif ($iheight && $iwidth / $iheight < 1 && !empty($pFileHash['max_width']) && !empty($pFileHash['max_height'])) { // we have a portrait image, flip everything $temp = $pFileHash['max_width']; $pFileHash['max_height'] = $pFileHash['max_width']; $pFileHash['max_width'] = round($iwidth / $iheight * $pFileHash['max_height']); } elseif (!empty($pFileHash['max_width'])) { $pFileHash['max_height'] = round($iheight / $iwidth * $pFileHash['max_width']); } elseif (!empty($pFileHash['max_height'])) { $pFileHash['max_width'] = round($iwidth / $iheight * $pFileHash['max_height']); } // Make sure not to scale up if ($pFileHash['max_width'] > $iwidth && $pFileHash['max_height'] > $iheight) { $pFileHash['max_width'] = $iwidth; $pFileHash['max_height'] = $iheight; } // override $mimeExt if we have a custom setting for it if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) { $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format'); } elseif ($itype = MagickGetImageMimeType($magickWand)) { list($type, $mimeExt) = preg_split('#/#', strtolower($itype)); } else { list($type, $mimeExt) = preg_split('#/#', strtolower($pFileHash['type'])); } $replaced = FALSE; $mimeExt = preg_replace("!^(x-)?(jpeg|png|gif)\$!", "\$2", $mimeExt, -1, $replaced); if ($replaced) { $targetType = $mimeExt; $destExt = '.' . $mimeExt; } if (empty($destExt) || $mimeExt == 'jpeg') { $targetType = 'jpeg'; $destExt = '.jpg'; } if (!empty($pFileHash['dest_file'])) { $destFile = $pFileHash['dest_file']; } else { $destFile = STORAGE_PKG_PATH . $pFileHash['dest_branch'] . $pFileHash['dest_base_name'] . $destExt; } if (!empty($pFileHash['max_width']) && !empty($pFileHash['max_height']) && ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight || $mimeExt != $targetType) || !empty($pFileHash['colorspace_conversion'])) { $pFileHash['name'] = $pFileHash['dest_base_name'] . $destExt; // Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html if ($error = liberty_magickwand_check_error(MagickResizeImage($magickWand, $pFileHash['max_width'], $pFileHash['max_height'], MW_CatromFilter, 1.0), $magickWand)) { $pFileHash['error'] .= $error; } if ($error = liberty_magickwand_check_error(MagickWriteImage($magickWand, $destFile), $magickWand)) { $pFileHash['error'] .= $error; } $pFileHash['size'] = filesize($destFile); } else { copy($pFileHash['source_file'], $destFile); } } $ret = $destFile; } else { $pFileHash['error'] = "No source file to resize"; } DestroyMagickWand($magickWand); return $ret; }
function resize_file_MagicWand(&$file, $create) { $image = NewMagickWand(); MagickReadImage($image, $this->upload->path . '/' . $this->orgFileName); MagickResizeImage($image, $this->newWidth, $this->newHeight, MW_MitchellFilter, 1); MagickSetImageCompressionQuality($image, $this->quality); //Set the extension of the new file $ext = $this->GetNewfileExtension(); if (file_exists($this->upload->path . '/' . $file->name . "." . $ext) and $file->name . "." . $ext != $file->fileName and $this->upload->nameConflict == "uniq") { $file->setFileName($this->upload->createUniqName($file->name . "." . $ext)); } if ($create == "image") { $fileName = $file->name . "." . $ext; @unlink($this->upload->path . '/' . $this->orgFileName); MagickWriteImage($image, $this->upload->path . '/' . $fileName); $file->setFileName($fileName); } else { if ($this->pathThumb == "") { $this->pathThumb = $this->upload->path; } if ($this->naming == "suffix") { $fileName = $file->name . $this->suffix . "." . $ext; } else { $fileName = $this->suffix . $file->name . "." . $ext; } MagickWriteImage($image, $this->pathThumb . '/' . $fileName); $file->setThumbFileName($fileName, $this->pathThumb, $this->naming, $this->suffix); } DestroyMagickWand($image); }
public function convertToJpg($srcFile, $desFile = "", $quality = 60) { if (empty($desFile)) { $desFile = str_replace(substr(strrchr($srcFile, "."), 1), "", $srcFile) . "jpg"; } $img = $this->getImgWand($srcFile); if (MagickGetNumberImages($img) > 1) { MagickResetIterator($img); MagickNextImage($img); $frame = $this->getImgWand(); MagickAddImage($frame, $img); DestroyMagickWand($img); $img = $frame; } MagickSetFormat($img, 'JPG'); MagickSetImageCompression($img, MW_JPEGCompression); MagickSetImageCompressionQuality($img, $quality); $this->generateImg($img, $desFile); return $desFile; }
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; }
/** * @return Imagens * @param string $digital * @param string $md5 * @param boolean $high * @param int $active * @param int $total */ public function createCacheJpegView($digital, $md5, $high = false) { $iImageHeightPixel = $high === false ? 960 : 2480; $iImageWidthPixel = $high === false ? 1280 : 3508; $lote = $this->generateLote($digital); $dirCache = sprintf('%s/cache/%s', __CAM_UPLOAD__, $lote); $tiff = sprintf('%s/%s/%s/%s.tif', __CAM_UPLOAD__, $lote, $digital, $md5); $view = sprintf('%s/%s/%s_view_%d.jpg', $dirCache, $digital, $md5, $high === false ? self::Q_LOW : self::Q_HIGH); $thumbs = sprintf('%s/%s/%s_thumb.jpg', $dirCache, $digital, $md5); if (!is_file($tiff)) { throw new Exception('Arquivo TIFF original não encontrado!'); } if (is_file($view)) { return $this; } // thumbs if (!is_file($thumbs)) { MagickReadImage($magickThumbs = NewMagickWand(), $tiff); if (MagickGetImageWidth($magickThumbs) < MagickGetImageHeight($magickThumbs)) { MagickResizeImage($magickThumbs, 150, 200, MW_QuadraticFilter, 1.0); } else { MagickResizeImage($magickThumbs, 200, 150, MW_QuadraticFilter, 1.0); } MagickSetImageFormat($magickThumbs, 'JPG'); MagickSetImageResolution($magickThumbs, 200, 200); MagickSetImageUnits($magickThumbs, MW_PixelsPerInchResolution); MagickSetImageCompression($magickThumbs, MW_JPEGCompression); MagickSetImageCompressionQuality($magickThumbs, 0.0); MagickWriteImage($magickThumbs, $thumbs); } // views MagickReadImage($magickView = NewMagickWand(), $tiff); if (MagickGetImageWidth($magickView) > MagickGetImageHeight($magickView)) { MagickResizeImage($magickView, $iImageWidthPixel, $iImageHeightPixel, MW_QuadraticFilter, 1.0); } else { MagickResizeImage($magickView, $iImageHeightPixel, $iImageWidthPixel, MW_QuadraticFilter, 1.0); } MagickSetImageFormat($magickView, 'JPG'); MagickSetImageResolution($magickView, 200, 200); MagickSetImageUnits($magickView, MW_PixelsPerInchResolution); MagickSetImageCompression($magickView, MW_JPEGCompression); MagickSetImageCompressionQuality($magickView, 0.0); MagickWriteImage($magickView, $view); $errorMagick = MagickGetExceptionString($magickView); if ($errorMagick) { throw new Exception($errorMagick); } return $this; }