function IMAGEN_tipo_normal() { $escalado = 'IMG/i/m/' . $_GET['ancho'] . '_' . $_GET['alto'] . '_' . $_GET['sha1']; $origen = 'IMG/i/' . $_GET['sha1']; $ancho = $_GET['ancho']; $alto = $_GET['alto']; if (@($ancho * $alto) > 562500) { die('La imagen solicitada excede el límite de este servicio'); } if (!file_exists($escalado)) { $im = new Imagick($origen); $im->setCompression(Imagick::COMPRESSION_JPEG); $im->setCompressionQuality(85); $im->setImageFormat('jpeg'); $im->stripImage(); $im->despeckleImage(); $im->sharpenImage(0.5, 1); //$im->reduceNoiseImage(0); $im->setInterlaceScheme(Imagick::INTERLACE_PLANE); $im->resizeImage($ancho, $alto, imagick::FILTER_LANCZOS, 1); $im->writeImage($escalado); $im->destroy(); } $im = new Imagick($escalado); $output = $im->getimageblob(); $outputtype = $im->getFormat(); $im->destroy(); header("Content-type: {$outputtype}"); header("Content-length: " . filesize($escalado)); echo $output; }
function mkbilder() { $this->err->write('mkbilder', 'IN'); if (!class_exists("Imagick")) { $this->err->out("Imagick-Extention nicht installiert", true); return false; } $handle = new Imagick(); if (!$handle->readImage("./tmp/tmp.file_org")) { return false; } $d = $handle->getImageGeometry(); if ($d["width"] < $d["height"]) { $faktor = $d["height"] / $d["width"]; } else { $faktor = $d["width"] / $d["height"]; } $thumbheight = floor($this->thumbwidth * $faktor); $handle->thumbnailImage($this->thumbwidth, $thumbheight); $rc = $handle->writeImage("./tmp/tmp.file_thumb"); $handle->readImage("./tmp/tmp.file_org"); $popupheight = floor($this->popupwidth * $faktor); $handle->thumbnailImage($this->popupwidth, $popupheight); $rc = $handle->writeImage("./tmp/tmp.file_popup"); $handle->readImage("./tmp/tmp.file_org"); $infoheight = floor($this->infowidth * $faktor); $handle->thumbnailImage($this->infowidth, $infoheight); $rc = $handle->writeImage("./tmp/tmp.file_info"); return $rc; }
public function index() { $ps = array(); $user = $this->session->all_userdata(); if (isset($_POST['submit']) && isset($user) && isset($user['users_id']) && isset($_FILES['uploaded_image']['tmp_name'])) { $imageFile = time() . '.' . end(explode(".", $_FILES['uploaded_image']['name'])); $photoData = $_POST['photo']; $image = new Imagick($_FILES['uploaded_image']['tmp_name']); $height = $image->getImageHeight(); $width = $image->getImageWidth(); if ($width > 800 || $height > 800) { if ($height < $width) { $image->scaleImage(800, 0); } else { $image->scaleImage(0, 600); } } $image->writeImage('./storage/photos/b/' . $imageFile); $image->cropThumbnailImage(100, 100); $image->writeImage('./storage/thumbs/' . $imageFile); $ps['photo_b'] = '/storage/photos/b/' . $imageFile; $ps['thumb'] = '/storage/thumbs/' . $imageFile; $data = array('iname' => $photoData['iname'] ? $photoData['iname'] : 'noname', 'idesc' => $photoData['idesc'] ? $photoData['idesc'] : '', 'path' => '/storage/photos/', 'file' => $imageFile, 'thumb' => $ps['thumb'], 'add_date' => time(), 'allow_comments' => isset($photoData['allow_comments']) ? 1 : 0, 'users_id' => $user['users_id']); $this->db->insert('photos', $data); $photos_id = $this->db->insert_id(); $this->load->model('m_users'); $this->load->model('m_logs'); $this->m_users->update(array('num_photos' => $user['num_photos'] + 1), $user['users_id']); $this->m_logs->add(array('users_id' => $user['users_id'], 'action' => 1, 'object_type' => 2, 'object_id' => $photos_id)); } $ps['_activeMenu'] = 'upload'; $this->tpl->view('frontend/upload/', $ps); }
function save($file_name = null, $quality = null) { $type = $this->out_type; if (!$type) { $type = $this->img_type; } if (!self::supportSaveType($type)) { throw new lmbImageTypeNotSupportedException($type); } $this->img->setImageFormat($type); $this->img->setImageFilename($file_name); if (!is_null($quality) && strtolower($type) == 'jpeg') { if (method_exists($this->img, 'setImageCompression')) { $this->img->setImageCompression(imagick::COMPRESSION_JPEG); $this->img->setImageCompressionQuality($quality); } else { $this->img->setCompression(imagick::COMPRESSION_JPEG); $this->img->setCompressionQuality($quality); } } if (!$this->img->writeImage($file_name)) { throw new lmbImageSaveFailedException($file_name); } $this->destroyImage(); }
/** * @see Fizzy_Image_Adapter_Interface */ public function save($destination) { //if(!is_writable($destination)) { // throw new Fizzy_Image_Exception("Destination '{$destination}' is not writable."); //} $this->_image->writeImage($destination); return $this; }
/** * @param $path */ public function save($path, $format = null, $quality = null) { if (!$format) { $format = "png"; } $this->resource->stripimage(); $this->resource->setImageFormat($format); if ($quality) { $this->resource->setCompressionQuality((int) $quality); $this->resource->setImageCompressionQuality((int) $quality); } $this->resource->writeImage($path); return $this; }
/** * {@inheritDoc} */ public function apply($absolutePath) { $info = pathinfo($absolutePath); if (isset($info['extension']) && false !== strpos(strtolower($info['extension']), 'pdf')) { // If it doesn't exists, extract the first page of the PDF if (!file_exists("{$absolutePath}.png")) { $this->imagick->readImage($absolutePath . '[0]'); $this->imagick->setImageFormat('png'); $this->imagick->writeImage("{$absolutePath}.png"); $this->imagick->clear(); } $absolutePath .= '.png'; } return $absolutePath; }
function imagick_thumbnail($image, $timage, $ext, $thumbnail_name, $imginfo) { try { $imagick = new Imagick(); $fullpath = "./" . $this->thumbnail_path . "/" . $timage[0] . "/" . $thumbnail_name; if ($ext == "gif") { $imagick->readImage($image . '[0]'); } else { $imagick->readImage($image); } $info = $imagick->getImageGeometry(); $this->info[0] = $info['width']; $this->info[1] = $info['height']; $imagick->thumbnailImage($this->dimension, $this->dimension, true); if ($ext == "png" || $ext == "gif") { $white = new Imagick(); $white->newImage($this->dimension, $this->dimension, "white"); $white->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0); $white->writeImage($fullpath); $white->clear(); } else { $imagick->writeImage($fullpath); } $imagick->clear(); } catch (Exception $e) { echo "Unable to load image." . $e->getMessage(); return false; } return true; }
/** * Write current image to a file * * @param string $file (OPTIONAL) The file to write to. Default: original file * @param string $type (OPTIONAL) The output image type. Default: jpeg * @return Engine_Image_Adapter_Gd * @throws Engine_Image_Adapter_Exception If unable to write */ public function write($file = null, $type = 'jpeg') { $this->_checkOpenImage(); // Set file type if ($type == 'jpg') { $type = 'jpeg'; } $type = strtoupper($type); if ($type !== $this->_resource->getImageFormat()) { $this->_resource->setImageFormat($type); } // Set quality if (null !== $this->_quality) { $this->_resource->setImageCompressionQuality($this->_quality); } // Write if (null === $file) { $return = $this->_resource->writeImage(); } else { $return = $this->_resource->writeImage($file); } // Error if (!$return) { if (!$file) { $file = $this->_resource->getImageFilename(); } throw new Engine_Image_Adapter_Exception(sprintf('Unable to write image to file "%s"', $file)); } return $this; }
public function makeThumb($path, $W = NULL, $H = NULL) { $image = new Imagick(); $image->readImage($ImgSrc); // Trasformo in RGB solo se e` il CMYK if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) { $image->transformImageColorspace(Imagick::COLORSPACE_RGB); } $image->profileImage('*', NULL); $image->stripImage(); $imgWidth = $image->getImageWidth(); $imgHeight = $image->getImageHeight(); if ((!$H || $H == null || $H == 0) && (!$W || $W == null || $W == 0)) { $W = $imgWidth; $H = $imgHeight; } elseif (!$H || $H == null || $H == 0) { $H = $W * $imgHeight / $imgWidth; } elseif (!$W || $W == null || $W == 0) { $W = $H * $imgWidth / $imgHeight; } $image->resizeImage($W, $H, Imagick::FILTER_LANCZOS, 1); /** Scrivo l'immagine */ $image->writeImage($path); /** IMAGE OUT */ header('X-MHZ-FLY: Nice job!'); header(sprintf('Content-type: image/%s', strtolower($image->getImageFormat()))); echo $image->getImageBlob(); $image->destroy(); die; }
public function createMissingThumbsAction() { $this->initCommonParams($this->getRequest()); $this->outputMessage('[light_cyan]Creating missing thumbs.'); $dbAdapter = $this->getServiceLocator()->get('dbAdapter'); $apartments = $dbAdapter->createStatement(' SELECT ai.*, a.`name` FROM `ga_apartment_images` ai LEFT JOIN `ga_apartments` a ON ai.`apartment_id` = a.`id` ')->execute(); foreach ($apartments as $apartment) { $this->outputMessage('[cyan]Looking into [light_blue]' . $apartment['name'] . '[cyan] images...'); for ($i = 1; $i <= 32; $i++) { if ($apartment['img' . $i]) { $imgPathComponents = explode('/', $apartment['img' . $i]); $originalFileName = array_pop($imgPathComponents); $originalFilePath = DirectoryStructure::FS_GINOSI_ROOT . DirectoryStructure::FS_IMAGES_ROOT . DirectoryStructure::FS_IMAGES_APARTMENT . $apartment['apartment_id'] . '/' . $originalFileName; $thumbFilePath = str_replace('_orig.', '_555.', $originalFilePath); if (!file_exists($originalFilePath)) { $this->outputMessage('[error] Original file for image ' . $i . ' is missing.'); } else { if (file_exists($thumbFilePath)) { $this->outputMessage('[light_blue] Thumb for image ' . $i . ' exists.'); } else { $thumb = new \Imagick($originalFilePath); $thumb->resizeImage(555, 370, \Imagick::FILTER_LANCZOS, 1); $thumb->writeImage($thumbFilePath); $this->outputMessage('[success] Added thumb for image ' . $i); } } } } } $this->outputMessage('Done!'); }
function upload_image($arr_image, $location, $compression = null, $width = 245, $height = 170) { $image_location = ""; $allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "GIF", "PNG", "pdf", "PDF"); $temp = explode(".", $arr_image["file"]["name"]); $extension = end($temp); if (($arr_image["file"]["type"] == "image/gif" || $arr_image["file"]["type"] == "image/jpeg" || $arr_image["file"]["type"] == "image/jpg" || $arr_image["file"]["type"] == "image/pjpeg" || $arr_image["file"]["type"] == "image/x-png" || $arr_image["file"]["type"] == "image/png") && $arr_image["file"]["size"] < 1024 * 1000 * 10 && in_array($extension, $allowedExts)) { if ($arr_image["file"]["error"] > 0) { echo "Return Code: " . $arr_image["file"]["error"] . "<br>"; } else { $compression_type = Imagick::COMPRESSION_JPEG; $image_location = $location . "." . $extension; if (move_uploaded_file($arr_image["file"]["tmp_name"], $image_location)) { //echo "Image Uploaded to : ".$image_location; } else { //echo "Image not uploaded"; } if (is_null($compression)) { $im = new Imagick($image_location); $im->setImageFormat('jpg'); $im->setImageCompression($compression_type); $im->setImageCompressionQuality(95); $im->stripImage(); $im->thumbnailImage($width, $height); $image_location = $location . ".jpg"; $im->writeImage($image_location); } } } return $image_location; }
/** * {@inheritdoc} */ public function save($file, $quality = 95) { // Set image quality $this->image->setImageCompressionQuality($quality); // Save image $this->image->writeImage($file); }
protected function save($path) { $pathinfo = pathinfo($path); if (!file_exists($pathinfo['dirname'])) { mkdir($pathinfo['dirname'], 0777, true); } try { $image = new \Imagick($this->source); $image->thumbnailImage($this->width, $this->height, true); if ($this->color) { $thumb = $image; $image = new \Imagick(); $image->newImage($this->width, $this->height, $this->color, $pathinfo['extension']); $size = $thumb->getImageGeometry(); $x = ($this->width - $size['width']) / 2; $y = ($this->height - $size['height']) / 2; $image->compositeImage($thumb, \imagick::COMPOSITE_OVER, $x, $y); $thumb->destroy(); } $image->writeImage($path); $image->destroy(); } catch (\Exception $e) { } return $this; }
public function crop(ImageInterface $image, Size $sizeDest, Point $cropIniPoint = null, $middleCrop = true) { $imagick = new \Imagick($image->getPath()); $widthSrc = $imagick->getImageWidth(); $heightSrc = $imagick->getImageHeight(); $widthDest = $sizeDest->getWidth(); $heightDest = $sizeDest->getHeight(); $cropX = 0; $cropY = 0; if ($middleCrop) { $cropX = ($widthSrc - $widthDest) / 2; $cropY = ($heightSrc - $heightDest) / 2; } else { if (isset($cropIniPoint)) { $cropX = $cropIniPoint->getX(); $cropY = $cropIniPoint->getY(); } } $imagick = new \Imagick($image->getPath()); $imagick->cropImage($widthDest, $heightDest, $cropX, $cropY); $addToName = "_croped_" . $widthDest . "x" . $heightDest; list($name, $extension) = explode(".", $image->getOriginalName()); list($path, $extension) = explode(".", $image->getPath()); $newPath = $path . $addToName . "." . $extension; $newName = $name . $addToName . "." . $extension; $newImageSize = new Size($imagick->getImageWidth(), $imagick->getImageHeight()); $imagick->writeImage($newPath); return $this->ImagickToImage($imagick, $image, $newPath, $newName, $newImageSize); }
function apply_mask($image_src_path, $mask_path, $image_dest_path) { $src = new Imagick($image_src_path); $mask = new Imagick($mask_path); $src->compositeImage($mask, Imagick::COMPOSITE_DEFAULT, 0, 0); $src->writeImage($image_dest_path); }
/** * @param $file * @return string */ public function thumbnail($file) { $format = ''; $name = md5((new \DateTime())->format('c')); foreach ($this->sizes as $size) { $image = new \Imagick($file); $imageRatio = $image->getImageWidth() / $image->getImageHeight(); $width = $size['width']; $height = $size['height']; $customRation = $width / $height; if ($customRation < $imageRatio) { $widthThumb = 0; $heightThumb = $height; } else { $widthThumb = $width; $heightThumb = 0; } $image->thumbnailImage($widthThumb, $heightThumb); $image->cropImage($width, $height, ($image->getImageWidth() - $width) / 2, ($image->getImageHeight() - $height) / 2); $image->setCompressionQuality(100); $format = strtolower($image->getImageFormat()); $pp = $this->cachePath . '/' . $size['name'] . "_{$name}." . $format; $image->writeImage($pp); } return "_{$name}." . $format; }
public function addWatermark($image_path) { try { // Open the original image $image = new \Imagick(); $is_success = $image->readImage($image_path); if ($is_success === FALSE) { throw new WatermarkException("Cannot read uploaded image"); } $watermark = new \Imagick(); $is_success = $watermark->readImage($this->_WATERMARK_IMAGE_PATH); if ($is_success === FALSE) { throw new WatermarkException("Cannot read uploaded image"); } $image_width = $image->getImageWidth(); $image_height = $image->getImageHeight(); $watermark_width = $watermark->getImageWidth(); $watermark_height = $watermark->getImageHeight(); $watermark_pos_x = $image_width - $watermark_width - 20; $watermark_pos_y = 20; // Overlay the watermark on the original image $is_success = $image->compositeImage($watermark, \imagick::COMPOSITE_OVER, $watermark_pos_x, $watermark_pos_y); if ($is_success === FALSE) { throw new WatermarkException("Cannot save image after watermarked"); } //Write the image $image->writeImage($image_path); } catch (ImagickException $e) { error_log($e->getMessage()); throw new WatermarkException($e->getMessage(), $e->getCode(), $e); } }
/** * Creates a thumbnail with imagick. * * @param File $fileObject FileObject to add properties * @param string $httpPathToMediaDir Http path to file * @param string $pathToMediaDirectory Local path to file * @param string $fileName Name of the image * @param string $fileExtension Fileextension * @param integer $width Width of thumbnail, if omitted, size will be proportional to height * @param integer $height Height of thumbnail, if omitted, size will be proportional to width * * @throws ThumbnailCreationFailedException If imagick is not supported * @throws InvalidArgumentException If both, height and width are omitted or file format is not supported */ private static function createThumbnailWithImagick(File &$fileObject, $httpPathToMediaDir, $pathToMediaDirectory, $fileName, $fileExtension, $width = null, $height = null) { if (!extension_loaded('imagick')) { throw new ExtensionNotLoadedException('Imagick is not loaded on this system'); } if ($width === null && $height === null) { throw new InvalidArgumentException('Either width or height must be provided'); } // create thumbnails with imagick $imagick = new \Imagick(); if (!in_array($fileExtension, $imagick->queryFormats("*"))) { throw new ThumbnailCreationFailedException('No thumbnail could be created for the file format'); } // read image into imagick $imagick->readImage(sprintf('%s/%s.%s', $pathToMediaDirectory, $fileName, $fileExtension)); // set size $imagick->thumbnailImage($width, $height); // null values allowed // write image $imagick->writeImage(sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $imagick->getImageWidth(), $imagick->getImageHeight(), $fileName, $fileExtension)); $fileObject->setThumbnailLink(sprintf('%s/%sx%s-thumbnail-%s.%s', $httpPathToMediaDir, $imagick->getImageWidth(), $imagick->getImageHeight(), $fileName, $fileExtension)); $fileObject->setLocalThumbnailPath(sprintf('%s/%sx%s-thumbnail-%s.%s', $pathToMediaDirectory, $imagick->getImageWidth(), $imagick->getImageHeight(), $fileName, $fileExtension)); // free up associated resources $imagick->destroy(); }
static function copyResizedImage($inputFile, $outputFile, $width, $height = null, $crop = true) { if (extension_loaded('gd')) { $image = new GD($inputFile); if ($height) { if ($width && $crop) { $image->cropThumbnail($width, $height); } else { $image->resize($width, $height); } } else { $image->resize($width); } return $image->save($outputFile); } elseif (extension_loaded('imagick')) { $image = new \Imagick($inputFile); if ($height && !$crop) { $image->resizeImage($width, $height, \Imagick::FILTER_LANCZOS, 1, true); } else { $image->resizeImage($width, null, \Imagick::FILTER_LANCZOS, 1); } if ($height && $crop) { $image->cropThumbnailImage($width, $height); } return $image->writeImage($outputFile); } else { throw new HttpException(500, 'Please install GD or Imagick extension'); } }
public function save() { $success = parent::save(); if ($success && isset($this->tempFile)) { $extension = $this->getExtension(); // save original $original = $this->name . "." . $extension; $path = $this->getPath(null, true); $this->logger->log("moving " . $this->tempFile["tmp_name"] . " to " . $path); $success = move_uploaded_file($this->tempFile["tmp_name"], $path); if ($success && $this->isImage()) { $thumbs = array(array('width' => 200, 'suffix' => 'small'), array('width' => 500, 'suffix' => 'med')); foreach ($thumbs as $thumb) { $thumb_path = $this->getPath($thumb['suffix'], true); $this->logger->log("thumbnailing " . $this->tempFile["tmp_name"] . " to " . $thumb_path); $image = new Imagick($path); $image->scaleImage($thumb['width'], 0); $image->writeImage($thumb_path); $image->destroy(); } } else { if (!$success) { $this->errorMessage = "Oops! We had trouble moving the file. Please try again later."; $success = false; } } } else { $this->status = "Sorry, there was an error with the file: " . $this->tempFile["error"]; $success = false; } return $success; }
public static function get($username) { $lowercase = strtolower($username); $skin = './minecraft/skins/' . $lowercase . '.png'; if (file_exists($skin)) { if (time() - filemtime($skin) < self::$expires) { return $lowercase; } $cached = true; } $binary = self::fetch('http://s3.amazonaws.com/MinecraftSkins/' . $username . '.png'); if ($binary === false) { if ($cached) { return $lowercase; } header('Status: 404 Not Found'); return $username == self::DEFAULT_SKIN ? $lowercase : self::get(self::DEFAULT_SKIN); } $img = new Imagick(); $img->readImageBlob($binary); $img->stripImage(); // strip metadata $img->writeImage($skin); self::_getHelm($img)->writeImage('./minecraft/helms/' . $lowercase . '.png'); self::_getHead($img)->writeImage('./minecraft/heads/' . $lowercase . '.png'); self::_getPlayer($img)->writeImage('./minecraft/players/' . $lowercase . '.png'); return $lowercase; }
/** * 保存图像 * @param string $imgname 图像保存名称 * @param string $type 图像类型 * @param boolean $interlace 是否对JPEG类型图像设置隔行扫描 * @throws \Exception */ public function save($imgname, $type = NULL, $interlace = true) { if (empty($this->img)) { throw new \Exception(_('No image resources can be saved')); } //设置图片类型 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); } }
function create_thumb($source_file, $thumb_file, $edge = THUMBNAIL_CROP_EDGE, &$src_w = NULL, &$src_h = NULL) { $composite_img = new Imagick($source_file); $blank_img = new Imagick(); $src_w = $composite_img->getImageWidth(); $src_h = $composite_img->getImageHeight(); if ($src_h > $edge && $src_w > $edge) { $composite_img->cropThumbnailImage($edge, $edge); $composite_img->setImageFormat('jpeg'); $composite_img->writeImage($thumb_file); $composite_img->clear(); $blank_img = $composite_img; } else { $blank_img->newImage($edge, $edge, new ImagickPixel('#DEF'), "jpg"); if ($src_w > $src_h) { $crop_x = $src_w / 2 - $edge / 2; $crop_y = 0; $offset_x = 0; $offset_y = $edge / 2 - $src_h / 2; } else { $crop_x = 0; $crop_y = $src_h / 2 - $edge / 2; $offset_x = $edge / 2 - $src_w / 2; $offset_y = 0; } $composite_img->cropImage($edge, $edge, $crop_x, $crop_y); $blank_img->compositeImage($composite_img, Imagick::COMPOSITE_OVER, $offset_x, $offset_y); $blank_img->setImageFormat('jpeg'); $blank_img->writeImage($thumb_file); $blank_img->clear(); } return $blank_img; }
/** * Saves the image to a file * * @param $filename string the name of the file to write to * * @return bool|PEAR_Error TRUE or a PEAR_Error object on error * @access public */ function save($filename, $type = '', $quality = null) { $options = is_array($quality) ? $quality : array(); if (is_numeric($quality)) { $options['quality'] = $quality; } $quality = $this->_getOption('quality', $options, 75); $this->imagick->setImageCompression($quality); if ($type && strcasecmp($type, $this->type)) { try { $this->imagick->setImageFormat($type); } catch (ImagickException $e) { return $this->raiseError('Could not save image to file (conversion failed).', IMAGE_TRANSFORM_ERROR_FAILED); } } try { $this->imagick->writeImage($filename); } catch (ImagickException $e) { return $this->raiseError('Could not save image to file: ' . $e->getMessage(), IMAGE_TRANSFORM_ERROR_IO); } if (!$this->keep_settings_on_save) { $this->free(); } return true; }
/** * Tries to converts the <b>$input</b> image into the <b>$output</b> format. * * @param string $input The input file. * @param string $output The output file. * @return void */ public static function convert($input, $output) { $inputType = strtolower(pathinfo($input, PATHINFO_EXTENSION)); $outputType = strtolower(pathinfo($output, PATHINFO_EXTENSION)); // Check for output file without extension and reuse input type if ($outputType === '') { $outputType = $inputType; $output .= ".{$outputType}"; } if ($inputType === 'svg') { self::prepareSvg($input); } if ($inputType === $outputType) { file_put_contents($output, file_get_contents($input)); } elseif (extension_loaded('imagick') === true) { $imagick = new \Imagick($input); $imagick->setImageFormat($outputType); $imagick->writeImage($output); // The following code is not testable when imagick is installed // @codeCoverageIgnoreStart } elseif (self::hasImagickConvert() === true) { $input = escapeshellarg($input); $output = escapeshellarg($output); system("convert {$input} {$output}"); } else { $fallback = substr($output, 0, -strlen($outputType)) . $inputType; echo "WARNING: Cannot generate image of type '{$outputType}'. This", " feature needs either the\n pecl/imagick extension or", " the ImageMagick cli tool 'convert'.\n\n", "Writing alternative image:\n{$fallback}\n\n"; file_put_contents($fallback, file_get_contents($input)); } // @codeCoverageIgnoreEnd }
function getPage($page) { $format = $this->parameters['format_image']; switch ($format) { case "imagick": case "png": $extension = "png"; $content_type = "image/x-png"; break; case "jpeg": $extension = "jpg"; $content_type = "image/jpeg"; break; } $len = strlen($this->getPageCount()); if (!file_exists($this->doc->driver->get_cached_filename($this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . "." . $extension)) { $resolution = $this->parameters['resolution_image']; if ($format == "imagick") { exec("pdftoppm -f {$page} -l {$page} -r " . $resolution . " " . $this->doc->driver->get_cached_filename($this->doc->id) . " " . $this->doc->driver->get_cached_filename("page_" . $this->doc->id)); $imagick = new Imagick(); $imagick->setResolution($resolution, $resolution); $imagick->readImage($this->doc->driver->get_cached_filename("page_" . $this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . ".ppm"); $imagick->writeImage($this->doc->driver->get_cached_filename("page_" . $this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . ".png"); unlink($this->doc->driver->get_cached_filename("page_" . $this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . ".ppm"); } else { exec("pdftoppm -f {$page} -l {$page} -r " . $resolution . " -" . $format . " " . $this->doc->driver->get_cached_filename($this->doc->id) . " " . $this->doc->driver->get_cached_filename("page_" . $this->doc->id)); } } if (file_exists($this->doc->driver->get_cached_filename("page_" . $this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . "." . $extension)) { header("Content-Type: " . $content_type); print file_get_contents($this->doc->driver->get_cached_filename("page_" . $this->doc->id) . "-" . str_pad($page, $len, "0", STR_PAD_LEFT) . "." . $extension); } }
public function beforeSave($options = array()) { if (isset($this->data['Candidate']['image']) && is_array($this->data['Candidate']['image'])) { if (!empty($this->data['Candidate']['image']['size'])) { $im = new Imagick($this->data['Candidate']['image']['tmp_name']); $im->resizeImage(512, 512, Imagick::FILTER_CATROM, 1, true); $path = WWW_ROOT . 'media'; $fileName = str_replace('-', '/', String::uuid()) . '.jpg'; if (!file_exists($path . '/' . dirname($fileName))) { mkdir($path . '/' . dirname($fileName), 0777, true); } $im->writeImage($path . '/' . $fileName); if (file_exists($path . '/' . $fileName)) { $this->data['Candidate']['image'] = $fileName; } else { unset($this->data['Candidate']['image']); } } else { unset($this->data['Candidate']['image']); } } if (isset($this->data['Candidate']['name'])) { $this->data['Candidate']['name'] = str_replace(array('&bull;', '•', '‧', '.', '•', '..'), '.', $this->data['Candidate']['name']); } return parent::beforeSave($options); }
public function resize($file, $size = array('width' => 100, 'height' => 100), $type = 'png', $fixed = false) { $image = new Imagick($this->file); $image->setBackgroundColor(new ImagickPixel('transparent')); $image->setImageFormat($type); if ($fixed === true) { $newWidth = $size['width']; $newHeight = $size['height']; } else { $imageprops = $image->getImageGeometry(); $width = $imageprops['width']; $height = $imageprops['height']; if ($width > $height) { $newHeight = $size['height']; $newWidth = $size['height'] / $height * $width; } else { $newWidth = $size['width']; $newHeight = $size['width'] / $width * $height; } } $image->resizeImage($newWidth, $newHeight, imagick::FILTER_LANCZOS, 1); $image->writeImage($file . '.' . $type); $image->clear(); $image->destroy(); }
private function watermarkPrint_imagick($src, $watermark, $dest_x, $dest_y, $quality, $transparency, $watermarkImgInfo) { try { // Open the original image $img = new Imagick($src); // Open the watermark $watermark = new Imagick($watermark); // Set transparency if (strtoupper($watermark->getImageFormat()) !== 'PNG') { $watermark->setImageOpacity($transparency / 100); } // Overlay the watermark on the original image $img->compositeImage($watermark, imagick::COMPOSITE_OVER, $dest_x, $dest_y); // Set quality if (strtoupper($img->getImageFormat()) === 'JPEG') { $img->setImageCompression(imagick::COMPRESSION_JPEG); $img->setCompressionQuality($quality); } $result = $img->writeImage($src); $img->clear(); $img->destroy(); $watermark->clear(); $watermark->destroy(); return $result ? true : false; } catch (Exception $e) { return false; } }