function resize($source_name, $width = "", $height = "", $save_name = "")
 {
     $resource = NewMagickWand();
     MagickReadImage($resource, $source_name);
     $src_image_x = MagickGetImageWidth($resource);
     $src_image_y = MagickGetImageHeight($resource);
     $src_image_scale = $src_image_x / $src_image_y;
     if ($width && $height) {
         $new_image_x = $width;
         $new_image_y = $height;
     } else {
         if ($width) {
             $new_image_x = $width;
             $new_image_y = $new_image_x * ($src_image_y / $src_image_x);
         } else {
             $new_image_y = $height;
             $new_image_x = $new_image_y * ($src_image_x / $src_image_y);
         }
     }
     MagickResizeImage($resource, $new_image_x, $new_image_y, MW_BoxFilter, 1);
     if ($save_name) {
         MagickWriteImage($resource, $save_name);
     } else {
         header('Content-Type: image/jpeg');
         MagickEchoImageBlob($resource);
     }
     DestroymagickWand($resource);
 }
예제 #2
0
 public function resize($src_file, $target_file, $width, $height, $type, $new_width, $new_height, $x = false, $y = false)
 {
     $rs = NewMagickWand();
     if (MagickReadImage($rs, $src_file)) {
         MagickResizeImage($rs, $new_width, $new_height, MW_QuadraticFilter, 0.3);
         MagickSetImageFormat($rs, 'image/jpeg');
         MagickWriteImage($rs, $target_file);
     }
     return true;
 }
예제 #3
0
 function image_resize(&$imgmdl, $src_file, $target_file, $new_width, $new_height)
 {
     if (isset($src_file) && is_file($src_file)) {
         list($width, $height, $type) = getimagesize($src_file);
         $size = self::get_image_size($new_width, $new_height, $width, $height);
         $new_width = $size[0];
         $new_height = $size[1];
         if (function_exists('magickresizeimage')) {
             $rs = NewMagickWand();
             if (MagickReadImage($rs, $src_file)) {
                 MagickResizeImage($rs, $new_width, $new_height, MW_QuadraticFilter, 0.3);
                 MagickSetImageFormat($rs, 'image/jpeg');
                 MagickWriteImage($rs, $target_file);
             }
             return true;
         } elseif (function_exists('imagecopyresampled')) {
             $quality = 80;
             $image_p = imagecreatetruecolor($new_width, $new_height);
             imagealphablending($image_p, false);
             switch ($type) {
                 case IMAGETYPE_JPEG:
                     $image = imagecreatefromjpeg($src_file);
                     $func = 'imagejpeg';
                     break;
                 case IMAGETYPE_GIF:
                     $image = imagecreatefromgif($src_file);
                     $func = 'imagegif';
                     break;
                 case IMAGETYPE_PNG:
                     $image = imagecreatefrompng($src_file);
                     imagesavealpha($image, true);
                     $func = 'imagepng';
                     $quality = 8;
                     break;
             }
             imagesavealpha($image_p, true);
             imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
             if ($func) {
                 $func($image_p, $target_file, $quality);
             }
             imagedestroy($image_p);
             imagedestroy($image);
         }
     }
 }
 /**
  * Do the actual resize of an image
  *
  * @param Asido_TMP &$tmp
  * @param integer $width
  * @param integer $height
  * @return boolean
  * @access protected
  */
 function __resize(&$tmp, $width, $height)
 {
     return MagickResizeImage($tmp->target, $width, $height, ASIDO_MW_RESIZE_FILTER, 0);
 }
예제 #5
0
/**
 * 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;
}
예제 #6
0
 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);
 }
예제 #7
0
 public function convert($ps_format, $ps_orig_filepath, $ps_dest_filepath)
 {
     $vs_filepath = $vs_ext = "";
     #
     # First make sure the original file is an image
     #
     if (!($vs_mimetype = $this->test($ps_orig_filepath, ""))) {
         return false;
     }
     if ($vs_mimetype == "application/pdf") {
         return false;
     }
     $va_dest_path_pieces = explode("/", $ps_dest_filepath);
     $vs_dest_filestem = array_pop($va_dest_path_pieces);
     $vs_dest_dir = join("/", $va_dest_path_pieces);
     $vn_width = $vn_height = null;
     switch ($ps_format) {
         # ------------------------------------
         case 'image/jpeg':
             $vs_ext = "jpg";
             $vs_filepath = $vs_dest_filestem . "_conv." . $vs_ext;
             switch ($this->backend) {
                 case LIBRARY_GD:
                     if ($vs_mimetype = $this->test($ps_orig_filepath)) {
                         switch ($vs_mimetype) {
                             case "image/jpeg":
                                 $rsource = imagecreatefromjpeg($ps_orig_filepath);
                                 break;
                             case "image/png":
                                 $rsource = imagecreatefrompng($ps_orig_filepath);
                                 break;
                             case "image/gif":
                                 $rsource = imagecreatefromgif($ps_orig_filepath);
                                 break;
                             default:
                                 return false;
                                 break;
                         }
                         if (!$r) {
                             return false;
                         }
                         list($vn_width, $vn_height) = getimagesize($ps_orig_filepath);
                         if ($vn_width > $vn_height) {
                             $vn_ratio = $vn_height / $vn_width;
                             $vn_target_width = 800;
                             $vn_target_height = 800 * $vn_ratio;
                         } else {
                             $vn_ratio = $vn_width / $vn_height;
                             $vn_target_height = 800;
                             $vn_target_width = 800 * $vn_ratio;
                         }
                         if (!($rdest = imagecreatetruecolor($vn_target_width, $vn_target_height))) {
                             return false;
                         }
                         if (!imagecopyresampled($rdest, $rsource, 0, 0, 0, 0, $vn_target_width, $vn_target_height, $vn_width, $vn_height)) {
                             return false;
                         }
                         if (!imagejpeg($rdest, $vs_dest_dir . "/" . $vs_filepath)) {
                             return false;
                         }
                     } else {
                         return false;
                     }
                     break;
                 default:
                     $handle = NewMagickWand();
                     if (MagickReadImage($handle, $ps_orig_filepath)) {
                         if (WandHasException($handle)) {
                             return false;
                         }
                         $vn_width = MagickGetImageWidth($handle);
                         $vn_height = MagickGetImageHeight($handle);
                         if ($vn_width > $vn_height) {
                             $vn_ratio = $vn_height / $vn_width;
                             $vn_target_width = 800;
                             $vn_target_height = 800 * $vn_ratio;
                         } else {
                             $vn_ratio = $vn_width / $vn_height;
                             $vn_target_height = 800;
                             $vn_target_width = 800 * $vn_ratio;
                         }
                         if (!MagickResizeImage($handle, $vn_target_width, $vn_target_height, MW_CubicFilter, 1)) {
                             return false;
                         }
                         if (!MagickWriteImage($handle, $vs_dest_dir . "/" . $vs_filepath)) {
                             return false;
                         }
                     }
                     break;
             }
             break;
             # ------------------------------------
         # ------------------------------------
         default:
             return false;
             break;
             # ------------------------------------
     }
     return array("extension" => $vs_ext, "format_name" => $this->info["CONVERSIONS"][$ps_format]["format_name"], "dangerous" => 0, "width" => $vn_width, "height" => $vn_height, "long_format_name" => $this->info["CONVERSIONS"][$ps_format]["long_format_name"]);
 }
예제 #8
0
파일: clip.php 프로젝트: syjzwjj/quyeba
 /**
  * 生成指定宽度和高度的图片
  * @param object image model object
  * @param string source file directory
  * @param mixed 临时数据源
  * @param string 宽度
  * @param string 高度
  * @return null 
  */
 function image_resize(&$imgmdl, $src_file, $target_file, $new_width, $new_height)
 {
     if (isset($src_file) && is_file($src_file)) {
         list($width, $height, $type) = getimagesize($src_file);
         $size = self::get_image_size($new_width, $new_height, $width, $height);
         $new_width = $size[0];
         $new_height = $size[1];
         if (ECAE_MODE) {
             include_lib('image.php');
             $obj = new ecae_image();
             $obj->set_file($src_file);
             $obj->resize($new_width, $new_height);
             $obj->strip();
             $content = $obj->exec();
             if ($content) {
                 file_put_contents($target_file, $content);
                 return true;
             } else {
                 return false;
             }
         } elseif (function_exists('magickresizeimage')) {
             $rs = NewMagickWand();
             if (MagickReadImage($rs, $src_file)) {
                 MagickResizeImage($rs, $new_width, $new_height, MW_QuadraticFilter, 0.3);
                 MagickSetImageFormat($rs, 'image/jpeg');
                 MagickWriteImage($rs, $target_file);
             }
             return true;
         } elseif (function_exists('imagecopyresampled')) {
             $quality = 80;
             $image_p = imagecreatetruecolor($new_width, $new_height);
             if ($new_width > $width && $new_height > $height) {
                 $background_color = imagecolorallocate($image_p, 255, 255, 255);
                 imagefilledrectangle($image_p, 0, 0, $new_width, $new_height, $background_color);
             }
             imagealphablending($image_p, false);
             switch ($type) {
                 case IMAGETYPE_JPEG:
                     $image = imagecreatefromjpeg($src_file);
                     $func = 'imagejpeg';
                     break;
                 case IMAGETYPE_GIF:
                     $image = imagecreatefromgif($src_file);
                     $func = 'imagegif';
                     break;
                 case IMAGETYPE_PNG:
                     $image = imagecreatefrompng($src_file);
                     imagesavealpha($image, true);
                     $func = 'imagepng';
                     $quality = 8;
                     break;
             }
             imagesavealpha($image_p, true);
             if ($new_width > $width && $new_height > $height) {
                 imagecopyresampled($image_p, $image, ($new_width - $width) / 2, ($new_height - $height) / 2, 0, 0, $width, $height, $width, $height);
             } else {
                 imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
             }
             if ($func) {
                 $func($image_p, $target_file, $quality);
             }
             imagedestroy($image_p);
             imagedestroy($image);
         }
     }
 }
예제 #9
0
 protected function _resize($newWidth, $newHeight)
 {
     MagickResizeImage($this->_resource, $newWidth, $newHeight, 7, 1);
     MagickUnsharpMaskImage($this->_resource, 0.5, 0.25, 1.2, 0.05);
 }
예제 #10
0
 /**
  * Image Process Using MagickWand
  *
  * This function will resize, crop or rotate
  *
  * @access	public
  * @auth	John Meng
  * @param	string
  * @return	bool
  */
 function image_process_magickwand($action = 'resize')
 {
     if (!file_exists($this->full_src_path)) {
         $this->set_error("Image source file not found!");
         return false;
     }
     if (file_exists($this->full_dst_path)) {
         @unlink("{$this->full_dst_path}");
     }
     $magick_wand = NewMagickWand();
     MagickRemoveImageProfiles($magick_wand);
     MagickSetCompressionQuality($magick_wand, $this->quality);
     MagickReadImage($magick_wand, $this->full_src_path);
     switch ($action) {
         case 'crop':
             MagickCropImage($magick_wand, $this->width, $this->height, $this->x_axis, $this->y_axis);
             break;
         case 'rotate':
             switch ($this->rotation_angle) {
                 case 90:
                     $angle = 90;
                     break;
                 case 180:
                     $angle = 180;
                     break;
                 case 270:
                     $angle = 270;
                     break;
                 case 'vrt':
                     $angle = 180;
                     break;
                 case 'hor':
                     $angle = 270;
                     break;
             }
             MagickRotateImage($magick_wand, null, $angle);
             break;
         case 'resize':
         default:
             MagickResizeImage($magick_wand, $this->width, $this->height, MW_LanczosFilter, 1.0);
             break;
     }
     MagickWriteImage($magick_wand, $this->full_dst_path);
     DestroymagickWand($magick_wand);
     // Set the file to 777
     @chmod($this->full_dst_path, $this->dir_write_mode);
     return TRUE;
 }
예제 #11
0
 /**
  * @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;
 }