/** * 保存图像 * @param string $imgname 图像保存名称 * @param string $type 图像类型 * @param integer $quality JPEG图像质量 * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描 */ public function save($imgname, $type = null, $quality = 80, $interlace = true) { if (empty($this->img)) { E('没有可以被保存的图像资源'); } //设置图片类型 if (is_null($type)) { $type = $this->info['type']; } else { $type = strtolower($type); $this->img->setImageFormat($type); } //JPEG图像设置隔行扫描 if ('jpeg' == $type || 'jpg' == $type) { $this->img->setImageInterlaceScheme(1); } // 设置图像质量 $this->img->setImageCompressionQuality($quality); //去除图像配置信息 $this->img->stripImage(); //保存图像 $imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径 if ('gif' == $type) { $this->img->writeImages($imgname, true); } else { $this->img->writeImage($imgname); } }
/** * 缩略(放大)图片 * @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; } self::$resource->setImageCompressionQuality(90); //1-100值越大,越清晰 return self::$resource->scaleImage($thumbWidth, $thumbHeight); }
/** * {@inheritdoc} */ public function write($file, $jpeg_quality = null) { if ($jpeg_quality) { $this->resource->setImageCompression(Imagick::COMPRESSION_JPEG); $this->resource->setImageCompressionQuality($jpeg_quality); } return $this->resource->writeImages($file, true); }
/** * Output imagick image to file * * @param resource $img imagick image resource * @param string $filename The path to save the file to. * @param string $destformat The Image type to use for $filename * @param int $jpgQuality JEPG quality (1-100) * @return bool */ protected function imagickImage($img, $filename, $destformat, $jpgQuality = null) { if (!$jpgQuality) { $jpgQuality = $this->options['jpgQuality']; } try { if ($destformat) { if ($destformat === 'gif') { $img->setImageFormat('gif'); } else { if ($destformat === 'png') { $img->setImageFormat('png'); } else { if ($destformat === 'jpg') { $img->setImageFormat('jpeg'); } } } } if (strtoupper($img->getImageFormat()) === 'JPEG') { $img->setImageCompression(imagick::COMPRESSION_JPEG); $img->setImageCompressionQuality($jpgQuality); try { $orientation = $img->getImageOrientation(); } catch (ImagickException $e) { $orientation = 0; } $img->stripImage(); if ($orientation) { $img->setImageOrientation($orientation); } } $result = $img->writeImage($filename); } catch (Exception $e) { $result = false; } return $result; }