Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('IMAGE_BORDER', 1);
- define('IMAGE_SCALE', 2);
- define('IMAGE_SQUASH', 3);
- define('IMAGE_CROP', 4);
- /*
- create_resource
- Creates a new resource for use by image_magick
- Returns: internal resource id
- load_image file name, resource id (default 1)
- Loads image data into memory
- Returns: nothing
- _load_image resource id (default 1)
- Loads full image into memory - usually called automatically
- Returns: nothing
- is_image resource id (default 1)
- Checks if resource is for a valid image
- Returns: false / image mime type
- get_dimensions resource id (default 1)
- Gets image dimensions
- Returns: array width & height
- get_size resource id (default 1)
- Gets image size
- Returns: byte size
- is_resource resource id (default false)
- Checks if resource exists
- Returns true / false;
- */
- class ims
- {
- function ims()
- {
- $this->resource = array();
- $this->image_loaded = array();
- $this->create_resource();
- //$tmp =& getInstance();
- //$this->config = $tmp->config;
- }
- function create_resource()
- {
- $resource_id = count($this->resource) + 1;
- $this->resource[$resource_id] = new Imagick();
- $this->image_loaded[$resource_id] = false;
- return $resource_id;
- }
- function load_image($file_name, $resource_id = 1)
- {
- $this->is_resource($resource_id);
- $this->filename = $file_name;
- // WE ONLY 1/2 LOAD THE IMAGE FOR PERFORMANCE
- // USE _LOAD_IMAGE FOR FULL LOAD
- $this->image_loaded[$resource_id] = $this->resource[$resource_id]->pingImage($this->filename) ?
- 'ping' : false;
- //$this->image_loaded[$resource_id] = MagickPingImage($this->resource[$resource_id], $this->filename) ? 'ping' : false;
- }
- function _load_image($resource_id = 1)
- {
- $this->is_resource($resource_id);
- $this->image_loaded[$resource_id] = $this->resource[$resource_id]->readImage($this->filename) ?
- 'full' : false;
- //$this->image_loaded[$resource_id] = MagickReadImage($this->resource[$resource_id], $this->filename) ? 'full' : false;
- }
- function is_image($resource_id = 1)
- {
- $this->is_resource($resource_id);
- return $this->image_loaded[$resource_id] ? $this->resource[$resource_id]->getImage() : false;
- //return $this->image_loaded[$resource_id] ? MagickGetImageMimeType($this->resource[$resource_id]) : false;
- }
- function get_dimensions($resource_id = 1)
- {
- if($this->image_loaded[$resource_id])
- {
- $dimensions['width'] = $this->resource[$resource_id]->getImageWidth();
- $dimensions['height'] = $this->resource[$resource_id]->getImageHeight();
- //$dimensions['width'] = MagickGetImageWidth($this->resource[$resource_id]);
- //$dimensions['height'] = MagickGetImageHeight($this->resource[$resource_id]);
- } else {
- $dimensions['width'] = false;
- $dimensions['height'] = false;
- }
- return $dimensions;
- }
- function get_size($resource_id = 1)
- {
- if($this->image_loaded[$resource_id])
- {
- //return MagickGetImageSize($this->resource[$resource_id]);
- return $this->resource[$resource_id]->getImageSize();
- } else {
- return 0;
- }
- }
- function is_resource($resource_id = false)
- {
- if(!isset($this->resource[$resource_id]))
- die('Resource Id: ' . $resource_id . ' does not exist');
- }
- function set_bg($resource_id, $colour) {
- }
- function resize_image($resource_id = 1, $width, $height, $resize_type = IMAGE_BORDER, $border_colour = false)
- {
- $this->is_resource($resource_id);
- $this->set_bg($resource_id, '#FFFFFF');
- if($this->image_loaded[$resource_id] != 'full') $this->_load_image($resource_id);
- if($resize_type == IMAGE_BORDER)
- {
- // CALCULATE NEW SIZE
- $new_dimensions = $this->calculate_size(
- $resource_id, $width, $height, 'minimum'
- );
- // SCALE THE IMAGE
- $this->_resize_image($resource_id, $new_dimensions);
- // CALCULATE BORDER OFFSET
- $top = ($height - $new_dimensions['height']) / 2;
- $left = ($width - $new_dimensions['width']) / 2;
- if($top == 0 && $left == 0)
- {
- } else {
- // ADD THE BORDERS
- $this->add_border($resource_id, $border_colour, $top, $left);
- }
- } else if($resize_type == IMAGE_CROP) {
- // CALCULATE NEW SIZE
- $new_dimensions = $this->calculate_size($resource_id, $width, $height, 'maximum');
- // SCALE THE IMAGE
- $this->_resize_image($resource_id, $new_dimensions);
- // CALCULATE BORDER OFFSET
- $top = ($new_dimensions['height'] - $height) / 2;
- $left = ($new_dimensions['width'] - $width) / 2;
- /*# if gif, save as jpeg, reload and crop
- if(MagickGetImageFormat($this->resource[$resource_id]) == 'GIF') {
- $outFile = $this->config->tmp . 'banners/b4b-gif-' . time();
- $this->output_image($resource_id, 'JPG', $outFile , 100);
- # re-read image
- $resource_id = $this->create_resource();
- $this->load_image($outFile, $resource_id);
- $this->_load_image($resource_id);
- unlink($outFile);
- }*/
- // CROP THE IMAGE
- $this->crop_image($resource_id, $width, $height, $top, $left);
- } else if($resize_type == IMAGE_SCALE) {
- // CALCULATE NEW SIZE
- $new_dimensions = $this->calculate_size($resource_id, $width, $height, 'maximum');
- // SCALE THE IMAGE
- $this->_resize_image($resource_id, $new_dimensions);
- } else if($resize_type == IMAGE_SQUASH) {
- $this->_resize_image($resource_id, array('width' => $width, 'height' => $height));
- } else {
- die('You specified a resize method that does not exists');
- }
- }
- function toJpeg($in, $out) {
- $res = $this->create_resource();
- $this->load_image($in, $res);
- $dimensions = $this->get_dimensions($res);
- $im = imagecreatetruecolor($dimensions['width'], $dimensions['height']);
- $col = imagecolorallocate($im, 255, 255, 255);
- imagefill($im, 0, 0, $col);
- $image = imagecreatefromstring(file_get_contents($in));
- imagecopyresampled(
- $im, $image, 0, 0, 0, 0, $dimensions['width'], $dimensions['height'],
- $dimensions['width'], $dimensions['height']
- );
- imagejpeg($im, $out, 100);
- imagedestroy($im);
- }
- function add_border($resource_id, $color, $top_border, $left_border)
- {
- $this->is_resource($resource_id);
- $this->resource[$resource_id]->borderImage($color, $left_border, $top_border);
- //MagickBorderImage($this->resource[$resource_id], $color, $left_border, $top_border);
- }
- function crop_image($resource_id, $width, $height, $x_offset, $y_offset)
- {
- $this->is_resource($resource_id);
- $this->resource[$resource_id]->cropImage($width, $height, round($y_offset, 0), round($x_offset, 0));
- //MagickCropImage($this->resource[$resource_id], $width, $height, round($y_offset, 0), round($x_offset, 0));
- }
- function output_image($resource_id, $type = 'JPG', $file_path = false, $quality = 80)
- {
- $valid_output_formats = array(
- 'JPG', 'JPEG', 'GIF', 'GIF87', 'PNG32', 'PNG24', 'PNG8'
- );
- $type = strtoupper($type);
- if($file_path)
- {
- // SET OUTPUT FORMAT
- if(in_array($type, $valid_output_formats))
- {
- if($type == 'JPG' || $type = 'JPEG')
- {
- $this->resource[$resource_id]->setImageCompression($quality);
- $this->resource[$resource_id]->setImageColorspace(13);
- //$this->resource[$resource_id]->setImageQuality($quality);
- //MagickSetImageCompression($this->resource[$resource_id], MW_JPEGCompression);
- //$this->resource[$resource_id]->setImageCompress($quality);
- //MagickSetImageCompressionQuality($this->resource[$resource_id], $quality);
- }
- if(file_exists($file_path))
- {
- unlink($file_path);
- }
- $response = $this->resource[$resource_id]->writeImage($file_path);
- //$response = MagickWriteImage($this->resource[$resource_id], $file_path);
- $this->clear($resource_id);
- return $response;
- } else {
- die('Invalid output format. Permitted: ' . implode(', ', $valid_output_formats) . '.');
- }
- } else {
- $this->resource[$resource_id]->setImageFormat('jpg');
- //MagickSetImageFormat($this->resource[$resource_id], 'jpg');
- echo $this->resource[$resource_id]->getImageBlob();
- //MagickEchoImageBlob($this->resource[$resource_id]);
- }
- }
- private function _resize_image($resource_id, $size)
- {
- $width = $size['width']; $height = $size['height'];
- //print_r($size); die();
- $this->resource[$resource_id]->scaleImage($width, $height);
- //MagickScaleImage($this->resource[$resource_id], $width, $height);
- }
- function clear($resource_id)
- {
- $this->is_resource($resource_id);
- $this->resource[$resource_id]->clear();
- //ClearMagickWand($this->resource[$resource_id]);
- unset($this->resource[$resource_id]);
- unset($this->image_loaded[$resource_id]);
- }
- function calculate_size($resource_id, $width, $height, $type = 'maximum')
- {
- $this->is_resource($resource_id);
- if($type == 'maximum')
- {
- $dimensions = $this->get_dimensions($resource_id);
- $scale = $dimensions['width'] / $width;
- if($scale == 0) $scale = 1;
- # check if height is going to be too small
- $nHeight = $dimensions['height'] / $scale;
- if($nHeight < $height) {
- $scale = $dimensions['height'] / $height;
- if($scale == 0) $scale = 1;
- }
- $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
- $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);
- } else if($type == 'minimum') {
- $dimensions = $this->get_dimensions($resource_id);
- $scale = $dimensions['width'] / $width;
- if(($dimensions['height'] / $scale) > $height)
- $scale = $dimensions['height'] / $height;
- if($scale == 0) $scale = 1;
- $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
- $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);
- /*$dimensions = $this->get_dimensions($resource_id);
- $scale = $dimensions['width'] / $width;
- if(($dimensions['height'] / $scale) < $height)
- $scale = $dimensions['height'] / $height;
- if($scale == 0) $scale = 1;
- $new_dimensions['width'] = round($dimensions['width'] / $scale, 0);
- $new_dimensions['height'] = round($dimensions['height'] / $scale, 0);*/
- } else {
- die('Size calculation type invalid');
- }
- return $new_dimensions;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement