Пример #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_magickwand_panorama_image - strictly speaking, this belongs in one of the image processing plugin files, but we'll leave it here for the moment
 *
 * @param array $pFileHash File hash - souce_file is required
 * @param array $pOptions
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_magickwand_panorama_image(&$pFileHash, $pOptions = array())
{
    $magickWand = NewMagickWand();
    $pFileHash['error'] = NULL;
    if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) {
        if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickReadImage($magickWand, $pFileHash['source_file']), $magickWand))) {
            // calculate border width
            $iwidth = round(MagickGetImageWidth($magickWand));
            $iheight = round(MagickGetImageHeight($magickWand));
            $aspect = $iwidth / $iheight;
            $metaHash = array('width' => $iwidth, 'height' => $iheight, 'aspect' => $aspect);
            // store original file information that we can adjust the viewer
            LibertyMime::storeMetaData($pFileHash['attachment_id'], 'PANO', $metaHash);
            // we need to pad the image if the aspect ratio is not 2:1 (give it a wee bit of leeway that we don't add annoying borders if not really needed)
            if ($aspect > 2.1 || $aspect < 1.9) {
                $bwidth = $bheight = 0;
                if ($aspect > 2) {
                    $bheight = round(($iwidth / 2 - $iheight) / 2);
                } else {
                    $bwidth = round(($iheight / 2 - $iwidth) / 2);
                }
                // if the ratio has nothing to do with a panorama image - i.e. gives us a negative number here, we won't generate a panorama image
                if ($bheight > 0) {
                    $pixelWand = NewPixelWand();
                    PixelSetColor($pixelWand, !empty($pOptions['background']) ? $pOptions['background'] : 'black');
                    if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickBorderImage($magickWand, $pixelWand, $bwidth, $bheight), $magickWand))) {
                        if (!($pFileHash['error'] = liberty_magickwand_check_error(MagickWriteImage($magickWand, $pFileHash['source_file']), $magickWand))) {
                            // yay!
                        }
                    }
                    DestroyPixelWand($pixelWand);
                }
            }
        }
    }
    DestroyMagickWand($magickWand);
    return empty($pFileHash['error']);
}
Пример #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;
}