Exemple #1
0
 public static function makeGifFromZip($zip_file_path, $delay)
 {
     $dir = $zip_file_path . 'dir/';
     $zip = new ZipArchive();
     $res = $zip->open($zip_file_path);
     if ($res === TRUE) {
         $zip->extractTo($dir);
         $zip->close();
     }
     $files = glob($dir . '/*');
     ksort($files);
     $mw = NewMagickWand();
     for ($i = 0, $l = count($files); $i < $l; $i++) {
         $rw = NewMagickWand();
         MagickReadImage($rw, $files[$i]);
         MagickSetImageDelay($rw, intval($delay) / 10);
         //magickwand比较特殊,>用的不是毫秒,所以毫秒需要转成1/100秒
         MagickAddImage($mw, $rw);
         DestroyMagickWand($rw);
     }
     MagickSetFormat($mw, 'gif');
     $gif_file_path = $zip_file_path . '.gif';
     MagickWriteImages($mw, $gif_file_path, true);
     DestroyMagickWand($mw);
     //todo 删除目录
     return $gif_file_path;
 }
 /**
  * 读取图片
  * @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);
         MagickReadImageFile(self::$resource, self::$fd);
         self::$resourcek = CloneMagickWand(self::$resource);
     } else {
         if (is_array($image)) {
             MagickNewImage(self::$resource, $image['width'], $image['height'], $image['backgroundColor']);
             MagickSetFormat(self::$resource, $image['format']);
         }
     }
 }
Exemple #3
0
/**
 * liberty_generate_thumbnails
 *
 * @param array $pFileHash
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_generate_thumbnails($pFileHash)
{
    global $gBitSystem, $gThumbSizes;
    $resizeFunc = liberty_get_function('resize');
    $ret = FALSE;
    // allow custom selection of thumbnail sizes
    if (empty($pFileHash['thumbnail_sizes'])) {
        if (!empty($gThumbSizes) && is_array($gThumbSizes)) {
            $pFileHash['thumbnail_sizes'] = array_keys($gThumbSizes);
        } else {
            $pFileHash['thumbnail_sizes'] = array('large', 'medium', 'small', 'avatar', 'icon');
        }
    }
    if (!preg_match('#image/(gif|jpe?g|png)#i', $pFileHash['type']) && $gBitSystem->isFeatureActive('liberty_jpeg_originals') || in_array('original', $pFileHash['thumbnail_sizes'])) {
        // jpeg version of original
        if (preg_match('/pdf/i', $pFileHash['type'])) {
            // has a customer pdf rasterization function been defined?
            if (function_exists('liberty_rasterize_pdf') && ($rasteredFile = liberty_rasterize_pdf($pFileHash['source_file']))) {
                $pFileHash['source_file'] = $rasteredFile;
            } else {
                $magickWand = NewMagickWand();
                if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand))) {
                    MagickSetFormat($magickWand, 'JPG');
                    if (MagickGetImageColorspace($magickWand) == MW_CMYKColorspace) {
                        MagickProfileImage($magickWand, "ICC", UTIL_PKG_PATH . 'icc/srgb.icm');
                        MagickSetImageColorspace($magickWand, MW_sRGBColorspace);
                    }
                    $imgWidth = MagickGetImageWidth($magickWand);
                    $imgHeight = MagickGetImageHeight($magickWand);
                    MagickSetImageUnits($magickWand, MW_PixelsPerInchResolution);
                    MagickSetResolution($magickWand, 300, 300);
                    $rasteredFile = dirname($pFileHash['source_file']) . '/original.jpg';
                    if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickWriteImage($magickWand, $rasteredFile), $magickWand))) {
                        $pFileHash['source_file'] = $rasteredFile;
                    }
                }
            }
        } else {
            $pFileHash['dest_base_name'] = 'original';
            $pFileHash['name'] = 'original.jpg';
            $pFileHash['max_width'] = MAX_THUMBNAIL_DIMENSION;
            $pFileHash['max_height'] = MAX_THUMBNAIL_DIMENSION;
            if ($convertedFile = $resizeFunc($pFileHash)) {
                $pFileHash['source_file'] = $convertedFile;
                $ret = TRUE;
            }
        }
        $pFileHash['type'] = $gBitSystem->verifyMimeType($pFileHash['source_file']);
    }
    // override $mimeExt if we have a custom setting for it
    if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) {
        $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format');
    } else {
        list($type, $mimeExt) = preg_split('#/#', strtolower($pFileHash['type']));
    }
    if (preg_match("!(png|gif)!", $mimeExt)) {
        $destExt = '.' . $mimeExt;
    } else {
        $destExt = '.jpg';
    }
    $initialDestPath = $pFileHash['dest_branch'];
    foreach ($pFileHash['thumbnail_sizes'] as $thumbSize) {
        if (isset($gThumbSizes[$thumbSize])) {
            $pFileHash['dest_base_name'] = $thumbSize;
            $pFileHash['name'] = $thumbSize . $destExt;
            if (!empty($gThumbSizes[$thumbSize]['width'])) {
                $pFileHash['max_width'] = $gThumbSizes[$thumbSize]['width'];
            } else {
                // Have to unset since we reuse $pFileHash
                unset($pFileHash['max_width']);
            }
            // reset dest_branch for created thumbs
            if (!empty($pFileHash['thumb_path'])) {
                $pFileHash['dest_file'] = $pFileHash['thumb_path'] . $pFileHash['name'];
            } else {
                // create a subdirectory for the thumbs
                $pFileHash['dest_branch'] = $initialDestPath . 'thumbs/';
                clearstatcache();
                if (!is_dir(STORAGE_PKG_PATH . $pFileHash['dest_branch'])) {
                    mkdir(STORAGE_PKG_PATH . $pFileHash['dest_branch'], 0775, TRUE);
                    clearstatcache();
                }
            }
            if (!empty($gThumbSizes[$thumbSize]['height'])) {
                $pFileHash['max_height'] = $gThumbSizes[$thumbSize]['height'];
            } else {
                // Have to unset since we reuse $pFileHash
                unset($pFileHash['max_height']);
            }
            if ($pFileHash['icon_thumb_path'] = $resizeFunc($pFileHash)) {
                $ret = TRUE;
                // use the previous thumb as the source for the next, decreasingly smaller thumb as this GREATLY increases speed
                $pFileHash['source_file'] = $pFileHash['icon_thumb_path'];
            }
        }
    }
    // to keep everything in bitweaver working smoothly, we need to remove the thumbs/ subdir again
    $pFileHash['dest_branch'] = $initialDestPath;
    return $ret;
}
 public function generateImg($img, $desFile = "")
 {
     MagickCommentImage($img, "Image Creator (MagickWand) By Windy2000");
     $type = MagickGetImageFormat($img);
     $frame_count = MagickGetNumberImages($img);
     if (empty($type)) {
         $type = $frame_count > 1 ? "GIF" : "JPG";
     }
     if (strpos("tile,gradient,caption,label,logo,netscape,rose", strtolower($type)) !== false) {
         $type = "PNG";
     }
     MagickSetFormat($img, $type);
     if (empty($desFile)) {
         //header("Content-Type: ".MagickGetMimeType($img));
         if ($frame_count > 1) {
             MagickEchoImagesBlob($img);
         } else {
             MagickEchoImageBlob($img);
         }
     } else {
         if ($frame_count > 1) {
             MagickWriteImages($img, $desFile, MagickTrue);
         } else {
             MagickWriteImage($img, $desFile);
         }
     }
     if (WandHasException($img)) {
         $this->Error($img);
     }
     $result = MagickGetExceptionType($img);
     DestroyMagickWand($img);
     return $result;
 }
Exemple #5
0
    }
    $font_size--;
}
$pre_morph_wnd =& checkWandError(MagickGetImage($mgck_wnd), $mgck_wnd, __LINE__);
addText($txt_wnd, $white, $font_size, $logo);
drawNewImage($pre_morph_wnd, $txt_wnd, $width, $height, $bg_color, __LINE__);
ClearDrawingWand($txt_wnd);
addText($txt_wnd, $white, $font_size, $welcome_msg);
drawNewImage($pre_morph_wnd, $txt_wnd, $width, $height, $bg_color, __LINE__);
MagickSetFirstIterator($mgck_wnd);
checkWandError(MagickAddImage($pre_morph_wnd, $mgck_wnd), $pre_morph_wnd, __LINE__);
addMorphedImages($mgck_wnd, $pre_morph_wnd, $num_morph_frames, $morph_delay, __LINE__);
DestroymagickWand($pre_morph_wnd);
DestroyDrawingWand($drw_wnd);
DestroyDrawingWand($txt_wnd);
checkWandError(MagickSetFormat($mgck_wnd, 'GIF'), $mgck_wnd, __LINE__);
header('Content-Type: ' . MagickGetMimeType($mgck_wnd));
MagickEchoImagesBlob($mgck_wnd);
DestroymagickWand($mgck_wnd);
/* ********** Function Declarations ********** */
/**
 * Function checkWandError() compares the value of $result, which should be
 * the result of any MagickWand API function, to explicit FALSE, and if it is
 * FALSE, checks if $wand for contains an error condition (in case the API
 * function is just returning FALSE as a normal return value).
 *
 * If the return value is FALSE, and the $wand contains a set error condition,
 * the function outputs the error and forcibly ends the program.
 *
 * If not, returns $result as a reference.
 *