/** * 保存图像 * @param string $imgname 图像保存名称 * @param string $type 图像类型 * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描 */ public function save($imgname, $type = null, $interlace = true) { if (empty($this->img)) { throw new Exception('没有可以被保存的图像资源'); } //设置图片类型 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->stripImage(); //保存图像 $imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径 if ('gif' == $type) { $this->img->writeImages($imgname, true); } else { $this->img->writeImage($imgname); } }
public function save($imgname, $type = NULL, $interlace = true) { if (empty($this->img)) { throw new Exception("没有可以被保存的图像资源"); } if (is_null($type)) { $type = $this->info["type"]; } else { $type = strtolower($type); $this->img->setImageFormat($type); } if ("jpeg" == $type || "jpg" == $type) { $this->img->setImageInterlaceScheme(1); } $this->img->stripImage(); $imgname = realpath(dirname($imgname)) . "/" . basename($imgname); if ("gif" == $type) { $this->img->writeImages($imgname, true); } else { $this->img->writeImage($imgname); } }
/** * {@inheritdoc} */ public function output($format = null) { if ($this->isBlank()) { throw new EmptyImageException('You are trying to output an empty image.'); } try { $format = $this->getFormat(); } catch (ImagickException $e) { $this->setFormat('jpg'); } if ($format !== null) { $this->resource->setImageFormat($format); } return $this->resource->getImagesBlob(); }
/** * 读取图片 * @param string $image 图片路径 */ public function setImage($image) { if (is_string($image)) { $opts = array('http' => array('timeout' => 5)); $context = stream_context_create($opts); $times = 0; do { self::$fd = fopen($image, 'r', $include_path = false, $context); if (++$times >= 3) { break; } } while (self::$fd === false); self::$resource->readImage(self::$fd); self::$resourcek = self::$resource->clone(); } else { if (is_array($image)) { self::$resource->newImage($image['width'], $image['height'], $image['backgroundColor']); self::$resource->setImageFormat($image['format']); } } }
/** * Add watermark text to image * * @param resource $image * @param array $opt * @return resource */ private function imageAddText($image, $color, $text) { if (osc_use_imagick()) { $draw = new ImagickDraw(); $draw->setFillColor('black'); //$color); $draw->setFont($this->font); $draw->setFontSize(30); $metrics = $image->queryFontMetrics($draw, $text); $geometry = $image->getImageGeometry(); switch (osc_watermark_place()) { case 'tl': $offset['x'] = 1; $offset['y'] = $metrics['ascender'] + 1; break; case 'tr': $offset['x'] = $geometry['width'] - $metrics['textWidth'] - 1; $offset['y'] = $metrics['ascender'] + 1; break; case 'bl': $offset['x'] = 1; $offset['y'] = $geometry['height'] - 1; break; case 'br': $offset['x'] = $geometry['width'] - $metrics['textWidth'] - 1; $offset['y'] = $geometry['height'] - 1; break; default: $offset['x'] = $geometry['width'] / 2 - $metrics['textWidth'] / 2; $offset['y'] = $geometry['height'] / 2 - $metrics['ascender'] / 2; break; } $image->annotateImage($draw, $offset['x'], $offset['y'], 0, $text); $image->setImageFormat('jpg'); } else { // allocate text color $color = $this->imageColorAllocateHex($image, $color); // calculate watermark position and get full path to font file $offset = $this->calculateOffset($image, $text); // Add the text to image imagettftext($image, 20, 0, $offset['x'], $offset['y'], $color, $this->font, html_entity_decode($text, null, "UTF-8")); } return $image; }
/** * 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; }