Example #1
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;
}
Example #2
0
/**
 * liberty_gd_resize_image
 *
 * @param array $pFileHash
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_gd_resize_image(&$pFileHash)
{
    global $gBitSystem;
    $ret = NULL;
    list($iwidth, $iheight, $itype, $iattr) = @getimagesize($pFileHash['source_file']);
    list($type, $ext) = explode('/', strtolower($pFileHash['type']));
    if (empty($pFileHash['max_width']) || empty($pFileHash['max_height']) || $iwidth <= $pFileHash['max_width'] && $iheight <= $pFileHash['max_height'] && ($ext == 'gif' || $ext == 'png' || $ext == 'jpg' || $ext == 'jpeg')) {
        // Keep the same dimensions as input file
        $pFileHash['max_width'] = $iwidth;
        $pFileHash['max_height'] = $iheight;
    } elseif ($iheight && $iwidth / $iheight > 0 && !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'] = $temp;
    }
    // we need to scale and/or reformat
    $fp = fopen($pFileHash['source_file'], "rb");
    $data = fread($fp, filesize($pFileHash['source_file']));
    fclose($fp);
    if (function_exists("ImageCreateFromString")) {
        $img = @imagecreatefromstring($data);
    }
    if (!empty($img)) {
        $size_x = imagesx($img);
        $size_y = imagesy($img);
    }
    if (!empty($img) && $size_x && $size_y) {
        if ($size_x > $size_y && !empty($pFileHash['max_width'])) {
            $tscale = (int) $size_x / $pFileHash['max_width'];
        } elseif (!empty($pFileHash['max_height'])) {
            $tscale = (int) $size_y / $pFileHash['max_height'];
        } else {
            $tscale = 1;
        }
        $tw = (int) ($size_x / $tscale);
        $ty = (int) ($size_y / $tscale);
        if (get_gd_version() > 1) {
            $t = imagecreatetruecolor($tw, $ty);
            imagesavealpha($t, TRUE);
            imagealphablending($t, FALSE);
            imagecopyresampled($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
        } else {
            $t = imagecreate($tw, $ty);
            //$imagegallib->ImageCopyResampleBicubic($t, $img, 0, 0, 0, 0, $tw, $ty, $size_x, $size_y);
        }
        // override $mimeExt if we have a custom setting for it
        if ($gBitSystem->isFeatureActive('liberty_thumbnail_format')) {
            $mimeExt = $gBitSystem->getConfig('liberty_thumbnail_format');
        } else {
            // make sure we have image_type_to_extension available
            include_once UTIL_PKG_PATH . 'PHP_Compat/Compat/Function/image_type_to_mime_type.php';
            list($type, $mimeExt) = explode('/', strtolower(image_type_to_mime_type($itype)));
        }
        if ($mimeExt = preg_replace("!^(x-)?(jpeg|png|gif)\$!", "\$2", $mimeExt)) {
            $targetType = $mimeExt;
            $destExt = '.' . $mimeExt;
        }
        if (!$mimeExt || $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;
        }
        switch ($targetType) {
            case 'png':
                if (imagetypes() & IMG_PNG) {
                    // png alpha stuff - needs more testing - spider
                    //     imagecolorallocatealpha ( $t, 0, 0, 0, 127 );
                    //     $ImgWhite = imagecolorallocate($t, 255, 255, 255);
                    //     imagefill($t, 0, 0, $ImgWhite);
                    //     imagecolortransparent($t, $ImgWhite);
                    imagepng($t, $destFile);
                    break;
                }
            case 'gif':
                // This must go immediately before default so default will be hit for PHP's without gif support
                if (imagetypes() & IMG_GIF) {
                    imagecolortransparent($t);
                    imagegif($t, $destFile);
                    break;
                }
            default:
                imagejpeg($t, $destFile);
                break;
        }
        // set permissions if possible - necessary for some wonky shared hosting environments
        if (chmod($pFileHash['source_file'], 0644)) {
            // does nothing, but fails elegantly
        }
        $pFileHash['name'] = $pFileHash['dest_base_name'] . $destExt;
        $pFileHash['size'] = filesize($destFile);
        $ret = $destFile;
    } elseif ($iwidth && $iheight) {
        $ret = liberty_process_generic($pFileHash, FALSE);
    }
    return $ret;
}
Example #3
0
/**
 * liberty_process_image
 *
 * @param array $pFileHash
 * @access public
 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
 */
function liberty_process_image(&$pFileHash, $pMoveFile = TRUE)
{
    global $gBitSystem;
    $ret = NULL;
    list($type, $ext) = explode('/', strtolower($pFileHash['type']));
    if ($resizePath = liberty_process_generic($pFileHash, $pMoveFile)) {
        $pFileHash['source_file'] = $resizePath;
        //set permissions if possible - necessary for some wonky shared hosting environments
        if (chmod($pFileHash['source_file'], 0644)) {
            //does nothing, but fails elegantly
        }
        $nameHold = $pFileHash['name'];
        $sizeHold = $pFileHash['size'];
        $ret = $pFileHash['source_file'];
        // do not thumbnail only if intentionally set to FALSE
        if (!isset($pFileHash['thumbnail']) || $pFileHash['thumbnail'] == TRUE) {
            liberty_generate_thumbnails($pFileHash);
        }
        $pFileHash['name'] = $nameHold;
        $pFileHash['size'] = $sizeHold;
    }
    return $ret;
}
Example #4
0
function liberty_imagick2_rotate_image(&$pFileHash)
{
    $ret = FALSE;
    if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) {
        $im = new Imagick();
        $im->readImage($pFileHash['source_file']);
        if (!$im->valid()) {
            $destFile = liberty_process_generic($pFileHash, FALSE);
        } elseif (empty($pFileHash['degrees']) || !is_numeric($pFileHash['degrees'])) {
            $pFileHash['error'] = tra('Invalid rotation amount');
        } else {
            $im->rotateImage(new ImagickPixel(), $pFileHash['degrees']);
            $im->writeImage($pFileHash['source_file']);
        }
    } else {
        $pFileHash['error'] = "No source file to resize";
    }
    return empty($pFileHash['error']);
}