コード例 #1
0
/**
 * liberty_magickwand_convert_colorspace
 * 
 * @param array $pFileHash
 * @param string $pColorSpace - target color space, only 'grayscale' is currently supported
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_magickwand_convert_colorspace_image(&$pFileHash, $pColorSpace)
{
    $ret = FALSE;
    if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) {
        $magickWand = NewMagickWand();
        if ($error = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand)) {
            bit_error_log("MagickReadImage Failed:{$error} ( {$pFileHash['source_file']} )");
        } else {
            MagickRemoveImageProfile($magickWand, "ICC");
            switch (strtolower($pColorSpace)) {
                case 'grayscale':
                    if (MagickGetImageColorspace($magickWand) == MW_GRAYColorspace) {
                        $ret = TRUE;
                    } else {
                        MagickSetImageColorspace($magickWand, MW_GRAYColorspace);
                        if (empty($pFileHash['dest_file'])) {
                            $pFileHash['dest_file'] = STORAGE_PKG_PATH . $pFileHash['dest_branch'] . $pFileHash['name'];
                        }
                        if ($error = liberty_magickwand_check_error(MagickWriteImage($magickWand, $pFileHash['dest_file']), $magickWand)) {
                            bit_error_log("MagickWriteImage Failed:{$error} ( {$pFileHash['source_file']} )");
                        } else {
                            $ret = TRUE;
                        }
                    }
                    break;
            }
        }
        DestroyMagickWand($magickWand);
    }
    return $ret;
}
コード例 #2
0
ファイル: liberty_lib.php プロジェクト: bitweaver/liberty
/**
 * 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;
}