Beispiel #1
0
 /**
  * Rotates the current image
  * Note: color mask are currently not supported
  *
  * @param   int     Rotation angle in degree
  * @param   array   No options are currently supported
  *
  * @return bool|PEAR_Error TRUE or a PEAR_Error object on error
  * @access public
  */
 function rotate($angle, $options = null)
 {
     if ($angle % 360 == 0) {
         return true;
     }
     if (!imagick_rotate($this->imageHandle, $angle)) {
         return $this->raiseError('Cannot create a new imagick image for the rotation.', IMAGE_TRANSFORM_ERROR_FAILED);
     }
     $this->new_x = imagick_getwidth($this->imageHandle);
     $this->new_y = imagick_getheight($this->imageHandle);
     return true;
 }
Beispiel #2
0
 function getimageinfo()
 {
     if ($this->uselib == "imagick") {
         $this->filetype = imagick_getmimetype($this->imagehandle);
         $this->xsize = imagick_getwidth($this->imagehandle);
         $this->ysize = imagick_getheight($this->imagehandle);
     } else {
         if ($this->uselib == "gd") {
             $this->xsize = imagesx($this->imagehandle);
             $this->ysize = imagesy($this->imagehandle);
         }
     }
 }
Beispiel #3
0
 /**
  * @return mixed
  */
 function get_height()
 {
     $this->_load_data();
     if ($this->data) {
         return imagick_getheight($this->data);
     }
 }
Beispiel #4
0
/**
 * Given a path under dataroot, an ID and a size, return the path to a file
 * matching all criteria.
 *
 * If the file with the ID exists but not of the correct size, this function
 * will make a copy that is resized to the correct size.
 *
 * @param string $path The base path in dataroot where the image is stored. For 
 *                     example, 'artefact/file/profileicons/' for profile 
 *                     icons
 * @param int $id      The ID of the image to return. Is typically the ID of an 
 *                     artefact
 * @param mixed $size  The size the image should be.
 *
 *                      As a two element hash with 'w' and 'h' keys:
 *                     - If 'w' and 'h' are not empty, the image will be 
 *                       exactly that size
 *                     - If just 'w' is not empty, the image will be that wide, 
 *                       and the height will be set to make the image scale 
 *                       correctly
 *                     - If just 'h' is not empty, the image will be that high, 
 *                       and the width will be set to make the image scale 
 *                       correctly
 *                     - If neither are set or the parameter is not set, the 
 *                       image will not be resized
 *
 *                     As a number, the path returned will have the largest side being 
 *                     the length specified.
 * @return string The path on disk where the appropriate file resides, or false 
 *                if an appropriate file could not be located or generated
 */
function get_dataroot_image_path($path, $id, $size = null)
{
    $dataroot = get_config('dataroot');
    $imagepath = $dataroot . $path;
    if (substr($imagepath, -1) == '/') {
        $imagepath = substr($imagepath, 0, -1);
    }
    if (!is_dir($imagepath) || !is_readable($imagepath)) {
        return false;
    }
    // Work out the location of the original image
    $originalimage = $imagepath . '/originals/' . $id % 256 . "/{$id}";
    // If the original has been deleted, then don't show any image, even a cached one.
    // delete_image only deletes the original, not any cached ones, so we have
    // to make sure the original is still around
    if (!is_readable($originalimage)) {
        return false;
    }
    if (!$size) {
        // No size has been asked for. Return the original
        return $originalimage;
    } else {
        // Check if the image is available in the size requested
        $sizestr = serialize($size);
        $md5 = md5("{$id}.{$sizestr}");
        $resizedimagedir = $imagepath . '/resized/';
        check_dir_exists($resizedimagedir);
        for ($i = 0; $i <= 2; $i++) {
            $resizedimagedir .= substr($md5, $i, 1) . '/';
            check_dir_exists($resizedimagedir);
        }
        $resizedimagefile = "{$resizedimagedir}{$md5}.{$id}";
        //.$sizestr";
        if (is_readable($resizedimagefile)) {
            return $resizedimagefile;
        }
        // Image is not available in this size. If there is a base image for
        // it, we can make one however.
        if (is_readable($originalimage)) {
            $imageinfo = getimagesize($originalimage);
            $originalmimetype = $imageinfo['mime'];
            switch ($originalmimetype) {
                case 'image/jpeg':
                case 'image/jpg':
                    $oldih = imagecreatefromjpeg($originalimage);
                    break;
                case 'image/png':
                    $oldih = imagecreatefrompng($originalimage);
                    break;
                case 'image/gif':
                    $oldih = imagecreatefromgif($originalimage);
                    break;
                case 'image/bmp':
                case 'image/x-bmp':
                case 'image/ms-bmp':
                case 'image/x-ms-bmp':
                    if (!extension_loaded('imagick')) {
                        log_info('Bitmap image detected for resizing, but imagick extension is not available');
                        return false;
                    }
                    $ih = imagick_readimage($originalimage);
                    if (!($newdimensions = image_get_new_dimensions(imagick_getwidth($ih), imagick_getheight($ih), $size))) {
                        return false;
                    }
                    imagick_resize($ih, $newdimensions['w'], $newdimensions['h'], IMAGICK_FILTER_LANCZOS, 1);
                    if (imagick_writeimage($ih, $resizedimagefile)) {
                        return $resizedimagefile;
                    }
                    return false;
                default:
                    return false;
            }
            if (!$oldih) {
                return false;
            }
            $oldx = imagesx($oldih);
            $oldy = imagesy($oldih);
            if (!($newdimensions = image_get_new_dimensions($oldx, $oldy, $size))) {
                return false;
            }
            $newih = imagecreatetruecolor($newdimensions['w'], $newdimensions['h']);
            if ($originalmimetype == 'image/png' || $originalmimetype == 'image/gif') {
                // Create a new destination image which is completely
                // transparent and turn off alpha blending for it, so that when
                // the PNG source file is copied, the alpha channel is retained.
                // Thanks to http://alexle.net/archives/131
                $background = imagecolorallocate($newih, 0, 0, 0);
                imagecolortransparent($newih, $background);
                imagealphablending($newih, false);
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                imagesavealpha($newih, true);
            } else {
                // imagecopyresized is faster, but results in noticeably worse image quality.
                // Given the images are resized only once each time they're
                // made, I suggest you just leave the good quality one in place
                imagecopyresampled($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
                //imagecopyresized($newih, $oldih, 0, 0, 0, 0, $newdimensions['w'], $newdimensions['h'], $oldx, $oldy);
            }
            $result = imagepng($newih, $resizedimagefile);
            if ($result) {
                return $resizedimagefile;
            }
        }
        // end attempting to build a resized image
    }
    // Image not available in any size
    return false;
}
 function get_height()
 {
     return imagick_getheight($this->data);
 }
Beispiel #6
0
function liberty_imagick0_resize_image(&$pFileHash)
{
    global $gBitSystem;
    $pFileHash['error'] = NULL;
    $ret = NULL;
    if (!empty($pFileHash['source_file']) && is_file($pFileHash['source_file'])) {
        $iImg = imagick_readimage($pFileHash['source_file']);
        if (!$iImg) {
            // $pFileHash['error'] = $pFileHash['name'].' '.tra ( "is not a known image file" );
            $destFile = liberty_process_generic($pFileHash, FALSE);
        } elseif (imagick_iserror($iImg)) {
            // $pFileHash['error'] = imagick_failedreason( $iImg ) . imagick_faileddescription( $iImg );
            $destFile = liberty_process_generic($pFileHash, FALSE);
        } else {
            imagick_set_image_quality($iImg, $gBitSystem->getConfig('liberty_thumbnail_quality', 85));
            $iwidth = imagick_getwidth($iImg);
            $iheight = imagick_getheight($iImg);
            if ($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;
            }
            $itype = imagick_getmimetype($iImg);
            // 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) = explode('/', strtolower($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['max_width']) && !empty($pFileHash['max_height']) && ($pFileHash['max_width'] < $iwidth || $pFileHash['max_height'] < $iheight || $mimeExt != $targetType)) {
                // We have to resize. *ALL* resizes are converted to jpeg or png
                if (!empty($pFileHash['dest_file'])) {
                    $destFile = $pFileHash['dest_file'];
                } else {
                    $destFile = STORAGE_PKG_PATH . $pFileHash['dest_branch'] . $pFileHash['dest_base_name'] . $destExt;
                }
                $pFileHash['name'] = $pFileHash['dest_base_name'] . $destExt;
                // print "			if ( !imagick_resize( $iImg, $pFileHash[max_width], $pFileHash[max_height], IMAGICK_FILTER_LANCZOS, 0.5, $pFileHash[max_width] x $pFileHash[max_height] > ) ) {";
                // Alternate Filter settings can seen here http://www.dylanbeattie.net/magick/filters/result.html
                if (!imagick_resize($iImg, $pFileHash['max_width'], $pFileHash['max_height'], IMAGICK_FILTER_CATROM, 1.0, '>')) {
                    $pFileHash['error'] .= imagick_failedreason($iImg) . imagick_faileddescription($iImg);
                }
                if (function_exists('imagick_set_attribute')) {
                    // this exists in the PECL package, but not php-imagick
                    $imagick_set_attribute($iImg, array("quality" => 1));
                }
                if (!imagick_writeimage($iImg, $destFile)) {
                    $pFileHash['error'] .= imagick_failedreason($iImg) . imagick_faileddescription($iImg);
                }
                $pFileHash['size'] = filesize($destFile);
            } else {
                // print "GENERIC";
                $destFile = liberty_process_generic($pFileHash, FALSE);
            }
        }
        $ret = $destFile;
    } else {
        $pFileHash['error'] = "No source file to resize";
    }
    return $ret;
}