Magento + Imagemagick Proportional Images - php

Implemented Imagemagick instead of GD2 in Magento 1.7.0.2 as suggested here:
http://www.magentocommerce.com/boards/v/viewthread/6158/P135/#t328638
However the new images are not proportional, causing stretching. They were perfect with GD2
Instructions:
In the Gd2.php file, in the save() function comment out
call_user_func_array($this->_getCallback('output'), $functionParameters);
This is the where Magento calls the imagejpeg() function. Yuck.
Now add the following code just below the commented out line.
$source_file = $this->_fileSrcPath.'/'.$this->_fileSrcName;
$image_info = getimagesize($source_file);
$im = new Imagick();
$im->readImage($source_file);
//check to see if image needs to be resized (eg. thumbnails)
if ($image_info[0] != $this->_imageSrcWidth) {
$im->thumbnailImage($this->_imageSrcWidth, $this->_imageSrcHeight);
}
$im->writeImage($fileName);
Here is the implemented code
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license#magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* #category Varien
* #package Varien_Image
* #copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* #license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class Varien_Image_Adapter_Gd2 extends Varien_Image_Adapter_Abstract
{
protected $_requiredExtensions = Array("gd");
private static $_callbacks = array(
IMAGETYPE_GIF => array('output' => 'imagegif', 'create' => 'imagecreatefromgif'),
IMAGETYPE_JPEG => array('output' => 'imagejpeg', 'create' => 'imagecreatefromjpeg'),
IMAGETYPE_PNG => array('output' => 'imagepng', 'create' => 'imagecreatefrompng'),
IMAGETYPE_XBM => array('output' => 'imagexbm', 'create' => 'imagecreatefromxbm'),
IMAGETYPE_WBMP => array('output' => 'imagewbmp', 'create' => 'imagecreatefromxbm'),
);
/**
* Whether image was resized or not
*
* #var bool
*/
protected $_resized = false;
public function open($filename)
{
$this->_fileName = $filename;
$this->getMimeType();
$this->_getFileAttributes();
$this->_imageHandler = call_user_func($this->_getCallback('create'), $this->_fileName);
}
public function save($destination=null, $newName=null)
{
$fileName = ( !isset($destination) ) ? $this->_fileName : $destination;
if( isset($destination) && isset($newName) ) {
$fileName = $destination . "/" . $newName;
} elseif( isset($destination) && !isset($newName) ) {
$info = pathinfo($destination);
$fileName = $destination;
$destination = $info['dirname'];
} elseif( !isset($destination) && isset($newName) ) {
$fileName = $this->_fileSrcPath . "/" . $newName;
} else {
$fileName = $this->_fileSrcPath . $this->_fileSrcName;
}
$destinationDir = ( isset($destination) ) ? $destination : $this->_fileSrcPath;
if( !is_writable($destinationDir) ) {
try {
$io = new Varien_Io_File();
$io->mkdir($destination);
} catch (Exception $e) {
throw new Exception("Unable to write file into directory '{$destinationDir}'. Access forbidden.");
}
}
if (!$this->_resized) {
// keep alpha transparency
$isAlpha = false;
$isTrueColor = false;
$this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
if ($isAlpha) {
if ($isTrueColor) {
$newImage = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
} else {
$newImage = imagecreate($this->_imageSrcWidth, $this->_imageSrcHeight);
}
$this->_fillBackgroundColor($newImage);
imagecopy(
$newImage,
$this->_imageHandler,
0, 0,
0, 0,
$this->_imageSrcWidth, $this->_imageSrcHeight
);
$this->_imageHandler = $newImage;
}
}
$functionParameters = array();
$functionParameters[] = $this->_imageHandler;
$functionParameters[] = $fileName;
// set quality param for JPG file type
if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_JPEG)
{
$functionParameters[] = $this->quality();
}
// set quality param for PNG file type
if (!is_null($this->quality()) && $this->_fileType == IMAGETYPE_PNG)
{
$quality = round(($this->quality() / 100) * 10);
if ($quality < 1) {
$quality = 1;
} elseif ($quality > 10) {
$quality = 10;
}
$quality = 10 - $quality;
$functionParameters[] = $quality;
}
//call_user_func_array($this->_getCallback('output'), $functionParameters);
$source_file = $this->_fileSrcPath.'/'.$this->_fileSrcName;
$image_info = getimagesize($source_file);
$im = new Imagick();
$im->readImage($source_file);
//check to see if image needs to be resized (eg. thumbnails)
if ($image_info[0] != $this->_imageSrcWidth) {
$im->thumbnailImage($this->_imageSrcWidth, $this->_imageSrcHeight);
}
$im->writeImage($fileName);
}
public function display()
{
header("Content-type: ".$this->getMimeType());
call_user_func($this->_getCallback('output'), $this->_imageHandler);
}
/**
* Obtain function name, basing on image type and callback type
*
* #param string $callbackType
* #param int $fileType
* #return string
* #throws Exception
*/
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
{
if (null === $fileType) {
$fileType = $this->_fileType;
}
if (empty(self::$_callbacks[$fileType])) {
throw new Exception($unsupportedText);
}
if (empty(self::$_callbacks[$fileType][$callbackType])) {
throw new Exception('Callback not found.');
}
return self::$_callbacks[$fileType][$callbackType];
}
private function _fillBackgroundColor(&$imageResourceTo)
{
// try to keep transparency, if any
if ($this->_keepTransparency) {
$isAlpha = false;
$transparentIndex = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
try {
// fill truecolor png with alpha transparency
if ($isAlpha) {
if (!imagealphablending($imageResourceTo, false)) {
throw new Exception('Failed to set alpha blending for PNG image.');
}
$transparentAlphaColor = imagecolorallocatealpha($imageResourceTo, 0, 0, 0, 127);
if (false === $transparentAlphaColor) {
throw new Exception('Failed to allocate alpha transparency for PNG image.');
}
if (!imagefill($imageResourceTo, 0, 0, $transparentAlphaColor)) {
throw new Exception('Failed to fill PNG image with alpha transparency.');
}
if (!imagesavealpha($imageResourceTo, true)) {
throw new Exception('Failed to save alpha transparency into PNG image.');
}
return $transparentAlphaColor;
}
// fill image with indexed non-alpha transparency
elseif (false !== $transparentIndex) {
$transparentColor = false;
if ($transparentIndex >=0 && $transparentIndex <= imagecolorstotal($this->_imageHandler)) {
list($r, $g, $b) = array_values(imagecolorsforindex($this->_imageHandler, $transparentIndex));
$transparentColor = imagecolorallocate($imageResourceTo, $r, $g, $b);
}
if (false === $transparentColor) {
throw new Exception('Failed to allocate transparent color for image.');
}
if (!imagefill($imageResourceTo, 0, 0, $transparentColor)) {
throw new Exception('Failed to fill image with transparency.');
}
imagecolortransparent($imageResourceTo, $transparentColor);
return $transparentColor;
}
}
catch (Exception $e) {
// fallback to default background color
}
}
list($r, $g, $b) = $this->_backgroundColor;
$color = imagecolorallocate($imageResourceTo, $r, $g, $b);
if (!imagefill($imageResourceTo, 0, 0, $color)) {
throw new Exception("Failed to fill image background with color {$r} {$g} {$b}.");
}
return $color;
}
/**
* Gives true for a PNG with alpha, false otherwise
*
* #param string $fileName
* #return boolean
*/
public function checkAlpha($fileName)
{
return ((ord(file_get_contents($fileName, false, null, 25, 1)) & 6) & 4) == 4;
}
private function _getTransparency($imageResource, $fileType, &$isAlpha = false, &$isTrueColor = false)
{
$isAlpha = false;
$isTrueColor = false;
// assume that transparency is supported by gif/png only
if ((IMAGETYPE_GIF === $fileType) || (IMAGETYPE_PNG === $fileType)) {
// check for specific transparent color
$transparentIndex = imagecolortransparent($imageResource);
if ($transparentIndex >= 0) {
return $transparentIndex;
}
// assume that truecolor PNG has transparency
elseif (IMAGETYPE_PNG === $fileType) {
$isAlpha = $this->checkAlpha($this->_fileName);
$isTrueColor = true;
return $transparentIndex; // -1
}
}
if (IMAGETYPE_JPEG === $fileType) {
$isTrueColor = true;
}
return false;
}
/**
* Change the image size
*
* #param int $frameWidth
* #param int $frameHeight
*/
public function resize($frameWidth = null, $frameHeight = null)
{
if (empty($frameWidth) && empty($frameHeight)) {
throw new Exception('Invalid image dimensions.');
}
// calculate lacking dimension
if (!$this->_keepFrame) {
if (null === $frameWidth) {
$frameWidth = round($frameHeight * ($this->_imageSrcWidth / $this->_imageSrcHeight));
}
elseif (null === $frameHeight) {
$frameHeight = round($frameWidth * ($this->_imageSrcHeight / $this->_imageSrcWidth));
}
}
else {
if (null === $frameWidth) {
$frameWidth = $frameHeight;
}
elseif (null === $frameHeight) {
$frameHeight = $frameWidth;
}
}
// define coordinates of image inside new frame
$srcX = 0;
$srcY = 0;
$dstX = 0;
$dstY = 0;
$dstWidth = $frameWidth;
$dstHeight = $frameHeight;
if ($this->_keepAspectRatio) {
// do not make picture bigger, than it is, if required
if ($this->_constrainOnly) {
if (($frameWidth >= $this->_imageSrcWidth) && ($frameHeight >= $this->_imageSrcHeight)) {
$dstWidth = $this->_imageSrcWidth;
$dstHeight = $this->_imageSrcHeight;
}
}
// keep aspect ratio
if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $frameWidth / $frameHeight) {
$dstHeight = round(($dstWidth / $this->_imageSrcWidth) * $this->_imageSrcHeight);
} else {
$dstWidth = round(($dstHeight / $this->_imageSrcHeight) * $this->_imageSrcWidth);
}
}
// define position in center (TODO: add positions option)
$dstY = round(($frameHeight - $dstHeight) / 2);
$dstX = round(($frameWidth - $dstWidth) / 2);
// get rid of frame (fallback to zero position coordinates)
if (!$this->_keepFrame) {
$frameWidth = $dstWidth;
$frameHeight = $dstHeight;
$dstY = 0;
$dstX = 0;
}
// create new image
$isAlpha = false;
$isTrueColor = false;
$this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha, $isTrueColor);
if ($isTrueColor) {
$newImage = imagecreatetruecolor($frameWidth, $frameHeight);
}
else {
$newImage = imagecreate($frameWidth, $frameHeight);
}
// fill new image with required color
$this->_fillBackgroundColor($newImage);
// resample source image and copy it into new frame
imagecopyresampled(
$newImage,
$this->_imageHandler,
$dstX, $dstY,
$srcX, $srcY,
$dstWidth, $dstHeight,
$this->_imageSrcWidth, $this->_imageSrcHeight
);
$this->_imageHandler = $newImage;
$this->refreshImageDimensions();
$this->_resized = true;
}
public function rotate($angle)
{
/*
$isAlpha = false;
$backgroundColor = $this->_getTransparency($this->_imageHandler, $this->_fileType, $isAlpha);
list($r, $g, $b) = $this->_backgroundColor;
if ($isAlpha) {
$backgroundColor = imagecolorallocatealpha($this->_imageHandler, 0, 0, 0, 127);
}
elseif (false === $backgroundColor) {
$backgroundColor = imagecolorallocate($this->_imageHandler, $r, $g, $b);
}
$this->_imageHandler = imagerotate($this->_imageHandler, $angle, $backgroundColor);
//*/
$this->_imageHandler = imagerotate($this->_imageHandler, $angle, $this->imageBackgroundColor);
$this->refreshImageDimensions();
}
public function watermark($watermarkImage, $positionX=0, $positionY=0, $watermarkImageOpacity=30, $repeat=false)
{
list($watermarkSrcWidth, $watermarkSrcHeight, $watermarkFileType, ) = getimagesize($watermarkImage);
$this->_getFileAttributes();
$watermark = call_user_func($this->_getCallback(
'create',
$watermarkFileType,
'Unsupported watermark image format.'
), $watermarkImage);
$merged = false;
if ($this->getWatermarkWidth() &&
$this->getWatermarkHeigth() &&
($this->getWatermarkPosition() != self::POSITION_STRETCH)
) {
$newWatermark = imagecreatetruecolor($this->getWatermarkWidth(), $this->getWatermarkHeigth());
imagealphablending($newWatermark, false);
$col = imagecolorallocate($newWatermark, 255, 255, 255);
imagecolortransparent($newWatermark, $col);
imagefilledrectangle($newWatermark, 0, 0, $this->getWatermarkWidth(), $this->getWatermarkHeigth(), $col);
imagealphablending($newWatermark, true);
imageSaveAlpha($newWatermark, true);
imagecopyresampled(
$newWatermark,
$watermark,
0, 0, 0, 0,
$this->getWatermarkWidth(), $this->getWatermarkHeigth(),
imagesx($watermark), imagesy($watermark)
);
$watermark = $newWatermark;
}
if( $this->getWatermarkPosition() == self::POSITION_TILE ) {
$repeat = true;
} elseif( $this->getWatermarkPosition() == self::POSITION_STRETCH ) {
$newWatermark = imagecreatetruecolor($this->_imageSrcWidth, $this->_imageSrcHeight);
imagealphablending($newWatermark, false);
$col = imagecolorallocate($newWatermark, 255, 255, 255);
imagecolortransparent($newWatermark, $col);
imagefilledrectangle($newWatermark, 0, 0, $this->_imageSrcWidth, $this->_imageSrcHeight, $col);
imagealphablending($newWatermark, true);
imageSaveAlpha($newWatermark, true);
imagecopyresampled(
$newWatermark,
$watermark,
0, 0, 0, 0,
$this->_imageSrcWidth, $this->_imageSrcHeight,
imagesx($watermark), imagesy($watermark)
);
$watermark = $newWatermark;
} elseif( $this->getWatermarkPosition() == self::POSITION_CENTER ) {
$positionX = ($this->_imageSrcWidth/2 - imagesx($watermark)/2);
$positionY = ($this->_imageSrcHeight/2 - imagesy($watermark)/2);
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
} elseif( $this->getWatermarkPosition() == self::POSITION_TOP_RIGHT ) {
$positionX = ($this->_imageSrcWidth - imagesx($watermark));
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
} elseif( $this->getWatermarkPosition() == self::POSITION_TOP_LEFT ) {
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
} elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_RIGHT ) {
$positionX = ($this->_imageSrcWidth - imagesx($watermark));
$positionY = ($this->_imageSrcHeight - imagesy($watermark));
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
} elseif( $this->getWatermarkPosition() == self::POSITION_BOTTOM_LEFT ) {
$positionY = ($this->_imageSrcHeight - imagesy($watermark));
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
}
if( $repeat === false && $merged === false ) {
imagecopymerge(
$this->_imageHandler,
$watermark,
$positionX, $positionY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
} else {
$offsetX = $positionX;
$offsetY = $positionY;
while( $offsetY <= ($this->_imageSrcHeight+imagesy($watermark)) ) {
while( $offsetX <= ($this->_imageSrcWidth+imagesx($watermark)) ) {
imagecopymerge(
$this->_imageHandler,
$watermark,
$offsetX, $offsetY,
0, 0,
imagesx($watermark), imagesy($watermark),
$this->getWatermarkImageOpacity()
);
$offsetX += imagesx($watermark);
}
$offsetX = $positionX;
$offsetY += imagesy($watermark);
}
}
imagedestroy($watermark);
$this->refreshImageDimensions();
}
public function crop($top=0, $left=0, $right=0, $bottom=0)
{
if( $left == 0 && $top == 0 && $right == 0 && $bottom == 0 ) {
return;
}
$newWidth = $this->_imageSrcWidth - $left - $right;
$newHeight = $this->_imageSrcHeight - $top - $bottom;
$canvas = imagecreatetruecolor($newWidth, $newHeight);
if ($this->_fileType == IMAGETYPE_PNG) {
$this->_saveAlpha($canvas);
}
imagecopyresampled(
$canvas,
$this->_imageHandler,
0, 0, $left, $top,
$newWidth, $newHeight,
$newWidth, $newHeight
);
$this->_imageHandler = $canvas;
$this->refreshImageDimensions();
}
public function checkDependencies()
{
foreach( $this->_requiredExtensions as $value ) {
if( !extension_loaded($value) ) {
throw new Exception("Required PHP extension '{$value}' was not loaded.");
}
}
}
private function refreshImageDimensions()
{
$this->_imageSrcWidth = imagesx($this->_imageHandler);
$this->_imageSrcHeight = imagesy($this->_imageHandler);
}
function __destruct()
{
#imagedestroy($this->_imageHandler);
}
/*
* Fixes saving PNG alpha channel
*/
private function _saveAlpha($imageHandler)
{
$background = imagecolorallocate($imageHandler, 0, 0, 0);
ImageColorTransparent($imageHandler, $background);
imagealphablending($imageHandler, false);
imagesavealpha($imageHandler, true);
}
}

Have you tried the hackathon magento imagemagick enabler?
[https://github.com/magento-hackathon/Perfect_Watermarks][1]
I am going to test this in a minute, so this suggestion is absolutly untested at the moment.

Related

How to change PHP thumbnail image size?

I found a PHP script online which helps me to create a thumbnail of an uploaded image, there is only one problem: I don't know how to change the outcome size of the thumbnail. I found the tutorial at: http://www.w3schools.in/php/image-upload-and-generate-thumbnail-using-ajax-in-php/
Can anyone help me to change the outcome size of the thumbnail?
Here is my code so far:
<?php
function createDir($path){
if (!file_exists($path)) {
$old_mask = umask(0);
mkdir($path, 0777, TRUE);
umask($old_mask);
}
}
function createThumb($path1, $path2, $file_type, $new_w, $new_h, $squareSize = ''){
/* read the source image */
$source_image = FALSE;
if (preg_match("/jpg|JPG|jpeg|JPEG/", $file_type)) {
$source_image = imagecreatefromjpeg($path1);
}
elseif (preg_match("/png|PNG/", $file_type)) {
if (!$source_image = #imagecreatefrompng($path1)) {
$source_image = imagecreatefromjpeg($path1);
}
}
elseif (preg_match("/gif|GIF/", $file_type)) {
$source_image = imagecreatefromgif($path1);
}
if ($source_image == FALSE) {
$source_image = imagecreatefromjpeg($path1);
}
$orig_w = imageSX($source_image);
$orig_h = imageSY($source_image);
if ($orig_w < $new_w && $orig_h < $new_h) {
$desired_width = $orig_w;
$desired_height = $orig_h;
} else {
$scale = min($new_w / $orig_w, $new_h / $orig_h);
$desired_width = ceil($scale * $orig_w);
$desired_height = ceil($scale * $orig_h);
}
if ($squareSize != '') {
$desired_width = $desired_height = $squareSize;
}
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
// for PNG background white----------->
$kek = imagecolorallocate($virtual_image, 255, 255, 255);
imagefill($virtual_image, 0, 0, $kek);
if ($squareSize == '') {
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $orig_w, $orig_h);
} else {
$wm = $orig_w / $squareSize;
$hm = $orig_h / $squareSize;
$h_height = $squareSize / 2;
$w_height = $squareSize / 2;
if ($orig_w > $orig_h) {
$adjusted_width = $orig_w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($virtual_image, $source_image, -$int_width, 0, 0, 0, $adjusted_width, $squareSize, $orig_w, $orig_h);
}
elseif (($orig_w <= $orig_h)) {
$adjusted_height = $orig_h / $wm;
$half_height = $adjusted_height / 2;
imagecopyresampled($virtual_image, $source_image, 0,0, 0, 0, $squareSize, $adjusted_height, $orig_w, $orig_h);
} else {
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $squareSize, $squareSize, $orig_w, $orig_h);
}
}
if (#imagejpeg($virtual_image, $path2, 90)) {
imagedestroy($virtual_image);
imagedestroy($source_image);
return TRUE;
} else {
return FALSE;
}
}
?>
the function gets called by the following lines of code:
<?php
include('./functions.php');
/*defined settings - start*/
ini_set("memory_limit", "99M");
ini_set('post_max_size', '20M');
ini_set('max_execution_time', 600);
define('IMAGE_SMALL_DIR', './uploades/small/');
define('IMAGE_SMALL_SIZE', 50);
define('IMAGE_MEDIUM_DIR', './uploades/medium/');
define('IMAGE_MEDIUM_SIZE', 250);
/*defined settings - end*/
if(isset($_FILES['image_upload_file'])){
$output['status']=FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png" );
if ($_FILES['image_upload_file']["error"] > 0) {
$output['error']= "Error in File";
}
elseif (!in_array($_FILES['image_upload_file']["type"], $allowedImageType)) {
$output['error']= "You can only upload JPG, PNG and GIF file";
}
elseif (round($_FILES['image_upload_file']["size"] / 1024) > 4096) {
$output['error']= "You can upload file size up to 4 MB";
} else {
/*create directory with 777 permission if not exist - start*/
createDir(IMAGE_SMALL_DIR);
createDir(IMAGE_MEDIUM_DIR);
/*create directory with 777 permission if not exist - end*/
$path[0] = $_FILES['image_upload_file']['tmp_name'];
$file = pathinfo($_FILES['image_upload_file']['name']);
$fileType = $file["extension"];
$desiredExt='jpg';
$fileNameNew = rand(333, 999) . time() . ".$desiredExt";
$path[1] = IMAGE_MEDIUM_DIR . $fileNameNew;
$path[2] = IMAGE_SMALL_DIR . $fileNameNew;
if (createThumb($path[0], $path[1], $fileType, IMAGE_MEDIUM_SIZE, IMAGE_MEDIUM_SIZE,IMAGE_MEDIUM_SIZE)) {
if (createThumb($path[1], $path[2],"$desiredExt", IMAGE_SMALL_SIZE, IMAGE_SMALL_SIZE,IMAGE_SMALL_SIZE)) {
$output['status']=TRUE;
$output['image_medium']= $path[1];
$output['image_small']= $path[2];
}
}
}
echo json_encode($output);
}
?>
I saw that you found the answer to your own problem. I wanted to add some additional insight.
The Right Way
Ideally you should be passing a param to the method as you can reset it if desired:
$imageSize = 100; // size
createThumb($path[0], $path[1], $fileType, $imageSize, $imageSize, $imageSize);
The Wrong Way
The script above is using constants. They are outlined with the define method. IE.
define('IMAGE_SMALL_SIZE', 50);
define('IMAGE_MEDIUM_SIZE', 250);
Change the number to your desired size. Ie
define('IMAGE_SMALL_SIZE', 150);
In this particular use case, the constants are still being passed into the function which defeats the need for a constant.

Custom image model not working

I have got a custom model for my images that should be able to locate the correct folder. And then let me resize images by doing this in model. example $this->model_tool_image->resize($this->settings->get('config_image'), 100, 100);
I know codeigniter has one but not what I am after.
For some reason not picking up images even though they are there. I think it may be a problem with the directories? All images are kept in base_url() . 'images/catalog' The posted image name can be got from DB fine.
Error: Could not load image ! Very strange I have defined path on model and it is working.
No images display
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_tool_image extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->library('images');
define('DIR_IMAGE', base_url('image/catalog') .'/');
}
public function resize($filename, $width, $height) {
if (!is_file(DIR_IMAGE . $filename)) {
return;
}
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$old_image = $filename;
$new_image = DIR_IMAGE . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image))) {
$path = '';
$directories = explode('/', dirname(str_replace('../', '', $new_image)));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir(DIR_IMAGE . $path)) {
#mkdir(DIR_IMAGE . $path, 0777);
}
}
list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);
if ($width_orig != $width || $height_orig != $height) {
$image = new Image(DIR_IMAGE . $old_image);
$image->resize($width, $height);
$image->save(DIR_IMAGE . $new_image);
} else {
copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
}
}
if ($this->input->server('HTTPS')) {
return HTTPS_SERVER . 'image/catalog/' . $new_image;
} else {
return HTTP_SERVER . 'image/catalog/' . $new_image;
}
}
}
Controller
$this->load->library('settings'); // Gets image from db OK example; nemo.png
$this->load->model('admin/tool/model_tool_image');
echo DIR_IMAGE;
if (!empty($this->input->post('config_image')) ) {
$data['config_image'] = $this->input->post('config_image');
} else {
$data['config_image'] = $this->settings->get('config_image');
}
if (!empty($this->input->post('config_image')) && is_file(DIR_IMAGE . $this->input->post('config_image'))) {
$data['thumb'] = $this->model_tool_image->resize($this->input->post('config_image'), 100, 100);
} elseif ($this->settings->get('config_image') && is_file(DIR_IMAGE . $this->settings->get('config_image'))) {
$data['thumb'] = $this->model_tool_image->resize($this->settings->get('config_image'), 100, 100);
} else {
$data['thumb'] = $this->model_tool_image->resize(base_url() . 'image/no_image.png', 100, 100);
}
$data['placeholder'] = $this->model_tool_image->resize(base_url() . 'image/no_image.png', 100, 100);
return $this->load->view('setting/settings', $data);
Custom Images Library
<?php
class Images {
private $file;
private $image;
private $info;
public function __construct($file = '') {
if (file_exists($file)) {
$this->file = $file;
$info = getimagesize($file);
$this->info = array(
'width' => $info[0],
'height' => $info[1],
'bits' => isset($info['bits']) ? $info['bits'] : '',
'mime' => isset($info['mime']) ? $info['mime'] : ''
);
$this->image = $this->create($file);
} else {
exit('Error: Could not load image ' . $file . '!');
}
}
private function create($image) {
$mime = $this->info['mime'];
if ($mime == 'image/gif') {
return imagecreatefromgif ($image);
} elseif ($mime == 'image/png') {
return imagecreatefrompng($image);
} elseif ($mime == 'image/jpeg') {
return imagecreatefromjpeg($image);
}
}
public function save($file, $quality = 90) {
$info = pathinfo($file);
$extension = strtolower($info['extension']);
if (is_resource($this->image)) {
if ($extension == 'jpeg' || $extension == 'jpg') {
imagejpeg($this->image, $file, $quality);
} elseif ($extension == 'png') {
imagepng($this->image, $file);
} elseif ($extension == 'gif') {
imagegif ($this->image, $file);
}
imagedestroy($this->image);
}
}
public function resize($width = 0, $height = 0, $default = '') {
if (!$this->info['width'] || !$this->info['height']) {
return;
}
$xpos = 0;
$ypos = 0;
$scale = 1;
$scale_w = $width / $this->info['width'];
$scale_h = $height / $this->info['height'];
if ($default == 'w') {
$scale = $scale_w;
} elseif ($default == 'h') {
$scale = $scale_h;
} else {
$scale = min($scale_w, $scale_h);
}
if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
return;
}
$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, 255, 255, 255);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
imagedestroy($image_old);
$this->info['width'] = $width;
$this->info['height'] = $height;
}
public function watermark($file, $position = 'bottomright') {
$watermark = $this->create($file);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
switch($position) {
case 'topleft':
$watermark_pos_x = 0;
$watermark_pos_y = 0;
break;
case 'topright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_y = 0;
break;
case 'bottomleft':
$watermark_pos_x = 0;
$watermark_pos_y = $this->info['height'] - $watermark_height;
break;
case 'bottomright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_y = $this->info['height'] - $watermark_height;
break;
}
imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
imagedestroy($watermark);
}
public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
$image_old = $this->image;
$this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
imagedestroy($image_old);
$this->info['width'] = $bottom_x - $top_x;
$this->info['height'] = $bottom_y - $top_y;
}
public function rotate($degree, $color = 'FFFFFF') {
$rgb = $this->html2rgb($color);
$this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
$this->info['width'] = imagesx($this->image);
$this->info['height'] = imagesy($this->image);
}
private function filter($filter) {
imagefilter($this->image, $filter);
}
private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
$rgb = $this->html2rgb($color);
imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
}
private function merge($file, $x = 0, $y = 0, $opacity = 100) {
$merge = $this->create($file);
$merge_width = imagesx($merge);
$merge_height = imagesy($merge);
imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
}
private function html2rgb($color) {
if ($color[0] == '#') {
$color = substr($color, 1);
}
if (strlen($color) == 6) {
list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
} elseif (strlen($color) == 3) {
list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
} else {
return false;
}
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}
}
define your DIR_IMAGE like this way at your model
define('DIR_IMAGE', FCPATH.'image/catalog/');
I dont think you schould use define like that. if you use a constant in a class you schould define it like this:
class MyClass
{
const MYCONSTANT = 'constant value';
function showConstant() {
echo self::MYCONSTANT. "\n";
}
}
Also i would recomand if you use a namespace capable php version to take a look at: http://php.net/manual/en/function.define.php
it is namespace defined, and also i am not sure if define works inside a class... tht is usualy in procedural code.

why is the output image filesize bigger than input using imagick?

why is the output image bigger than input using imagick?
original jpg: http://www.persunmall.com/images/jm3.jpg
/**
* example
--------------------------------------
'jm3.jpg' => 119 k (453*680)
'l.jpg' => 275 k (400*600) // why the output jpg is much bigger than input jpg?
*/
$jy_image=new jy_image
$jy_image->output_image('jm3.jpg','l.jpg',400,0);
class bellow:
<?php
class jy_image {
private $image = null;
private $type = null;
// 构造函数
public function __construct() {
}
// 析构函数
public function __destruct() {
if ($this->image !== null)
$this->image->destroy();
}
// 载入图像
public function open($path) {
$this->image = new Imagick($path);
if ($this->image) {
$this->type = strtolower($this->image->getImageFormat()); // jpeg
}
return $this->image;
}
public function crop($x = 0, $y = 0, $width = null, $height = null) {
if ($width == null)
$width = $this->image->getImageWidth() - $x;
if ($height == null)
$height = $this->image->getImageHeight() - $y;
if ($width <= 0 || $height <= 0)
return;
if ($this->type == 'gif') {
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach ($images as $frame) {
$img = new Imagick();
$img->readImageBlob($frame);
$img->cropImage($width, $height, $x, $y);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
$canvas->setImagePage($width, $height, 0, 0);
}
$image->destroy();
$this->image = $canvas;
} else {
$this->image->cropImage($width, $height, $x, $y);
}
}
/*
* 更改图像大小
$fit: 适应大小方式
'force': 把图片强制变形成 $width X $height 大小
'scale': 按比例在安全框 $width X $height 内缩放图片, 输出缩放后图像大小 不完全等于 $width X $height
'scale_fill': 按比例在安全框 $width X $height 内缩放图片,安全框内没有像素的地方填充色, 使用此参数时可设置背景填充色 $bg_color = array(255,255,255)(红,绿,蓝, 透明度) 透明度(0不透明-127完全透明))
其它: 智能模能 缩放图像并载取图像的中间部分 $width X $height 像素大小
$fit = 'force','scale','scale_fill' 时: 输出完整图像
$fit = 图像方位值 时, 输出指定位置部分图像
字母与图像的对应关系如下:
north_west north north_east
west center east
south_west south south_east
*/
public function resize_to($width = 100, $height = 100, $fit = 'center', $fill_color = array(255, 255, 255, 0)) {
// 保证其中一边,另外一边相应缩放
if($width==0 || $height==0){
$image = $this->image;
$src_width = $image->getImageWidth();
$src_height = $image->getImageHeight();
if($width>0 && $height==0){
$width=($width>$src_width)?$src_width:$width;
$height = $src_height*($width/$src_width);
}
else if($width==0 && $height>0){
$height=($height>$src_height)?$src_height:$height;
$width = $src_width*($height/$src_height);
}
$fit='force';
}
switch ($fit) {
case 'force': // 强制缩放,会变形
if ($this->type == 'gif') {
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach ($images as $frame) {
$img = new Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage($width, $height, false);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
}
$image->destroy();
$this->image = $canvas;
} else {
$this->image->thumbnailImage($width, $height, false);
}
break;
case 'scale':
if ($this->type == 'gif') {
$image = $this->image;
$images = $image->coalesceImages();
$canvas = new Imagick();
foreach ($images as $frame) {
$img = new Imagick();
$img->readImageBlob($frame);
$img->thumbnailImage($width, $height, true);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
}
$image->destroy();
$this->image = $canvas;
} else {
$this->image->thumbnailImage($width, $height, true);
}
break;
case 'scale_fill': // 填充白色
$size = $this->image->getImagePage();
$src_width = $size['width'];
$src_height = $size['height'];
$x = 0;
$y = 0;
$dst_width = $width;
$dst_height = $height;
if ($src_width * $height > $src_height * $width) {
$dst_height = intval($width * $src_height / $src_width);
$y = intval(($height - $dst_height) / 2);
} else {
$dst_width = intval($height * $src_width / $src_height);
$x = intval(($width - $dst_width) / 2);
}
$image = $this->image;
$canvas = new Imagick();
$color = 'rgba(' . $fill_color[0] . ',' . $fill_color[1] . ',' . $fill_color[2] . ',' . $fill_color[3] . ')';
if ($this->type == 'gif') {
$images = $image->coalesceImages();
foreach ($images as $frame) {
$frame->thumbnailImage($width, $height, true);
$draw = new ImagickDraw();
$draw->composite($frame->getImageCompose(), $x, $y, $dst_width, $dst_height, $frame);
$img = new Imagick();
$img->newImage($width, $height, $color, 'gif');
$img->drawImage($draw);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
$canvas->setImagePage($width, $height, 0, 0);
}
} else {
$image->thumbnailImage($width, $height, true);
$draw = new ImagickDraw();
$draw->composite($image->getImageCompose(), $x, $y, $dst_width, $dst_height, $image);
$canvas->newImage($width, $height, $color, $this->get_type());
$canvas->drawImage($draw);
$canvas->setImagePage($width, $height, 0, 0);
}
$image->destroy();
$this->image = $canvas;
break;
default:
$size = $this->image->getImagePage();
$src_width = $size['width'];
$src_height = $size['height'];
$crop_x = 0;
$crop_y = 0;
$crop_w = $src_width;
$crop_h = $src_height;
if ($src_width * $height > $src_height * $width) {
$crop_w = intval($src_height * $width / $height); // 100*50 =>50*100 输出:25*50
} else {
$crop_h = intval($src_width * $height / $width); //50*100 =>100*50 输出:50*25
}
switch ($fit) {
case 'north_west':
$crop_x = 0;
$crop_y = 0;
break;
case 'north':
$crop_x = intval(($src_width - $crop_w) / 2);
$crop_y = 0;
break;
case 'north_east':
$crop_x = $src_width - $crop_w;
$crop_y = 0;
break;
case 'west':
$crop_x = 0;
$crop_y = intval(($src_height - $crop_h) / 2);
break;
case 'center':
$crop_x = intval(($src_width - $crop_w) / 2);
$crop_y = intval(($src_height - $crop_h) / 2);
break;
case 'east':
$crop_x = $src_width - $crop_w;
$crop_y = intval(($src_height - $crop_h) / 2);
break;
case 'south_west':
$crop_x = 0;
$crop_y = $src_height - $crop_h;
break;
case 'south':
$crop_x = intval(($src_width - $crop_w) / 2);
$crop_y = $src_height - $crop_h;
break;
case 'south_east':
$crop_x = $src_width - $crop_w;
$crop_y = $src_height - $crop_h;
break;
default:
$crop_x = intval(($src_width - $crop_w) / 2);
$crop_y = intval(($src_height - $crop_h) / 2);
}
$image = $this->image;
$canvas = new Imagick();
if ($this->type == 'gif') {
$images = $image->coalesceImages();
foreach ($images as $frame) {
$img = new Imagick();
$img->readImageBlob($frame);
$img->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
$img->thumbnailImage($width, $height, true);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
$canvas->setImagePage($width, $height, 0, 0);
}
} else {
$image->cropImage($crop_w, $crop_h, $crop_x, $crop_y);
$image->thumbnailImage($width, $height, true);
$canvas->addImage($image);
$canvas->setImagePage($width, $height, 0, 0);
}
$image->destroy();
$this->image = $canvas;
}
}
// 添加水印图片
public function add_watermark($path, $x = 0, $y = 0) {
$watermark = new Imagick($path);
// 默认水印位置 by jimmy 2014-4-11
if(empty($x) && empty($y)){
$im_w= $this->image->getImageWidth();
$im_h = $this->image->getImageHeight();
$wa_w = $watermark->getImageWidth();
$wa_h = $watermark->getImageHeight();
$x=($im_w-$wa_w)/2;
$y=($im_h-$wa_h-50);
}
$draw = new ImagickDraw();
$draw->composite($watermark->getImageCompose(), $x, $y, $watermark->getImageWidth(), $watermark->getimageheight(), $watermark);
if ($this->type == 'gif') {
$image = $this->image;
$canvas = new Imagick();
$images = $image->coalesceImages();
foreach ($image as $frame) {
$img = new Imagick();
$img->readImageBlob($frame);
$img->drawImage($draw);
$canvas->addImage($img);
$canvas->setImageDelay($img->getImageDelay());
}
$image->destroy();
$this->image = $canvas;
} else {
$this->image->drawImage($draw);
}
}
// 添加水印文字
public function add_text($text, $x = 0, $y = 0, $angle = 0, $style = array()) {
$draw = new ImagickDraw();
if (isset($style['font']))
$draw->setFont($style['font']);
if (isset($style['font_size']))
$draw->setFontSize($style['font_size']);
if (isset($style['fill_color']))
$draw->setFillColor($style['fill_color']);
if (isset($style['under_color']))
$draw->setTextUnderColor($style['under_color']);
if ($this->type == 'gif') {
foreach ($this->image as $frame) {
$frame->annotateImage($draw, $x, $y, $angle, $text);
}
} else {
$this->image->annotateImage($draw, $x, $y, $angle, $text);
}
}
// 保存到指定路径
public function save_to($path) {
$this->image->stripImage();
if ($this->type == 'gif') {
$this->image->writeImages($path, true);
} else {
$this->image->writeImage($path);
}
}
// 输出图像
public function output($header = true) {
if ($header)
header('Content-type: ' . $this->type);
echo $this->image->getImagesBlob();
}
public function get_width() {
$size = $this->image->getImagePage();
return $size['width'];
}
public function get_height() {
$size = $this->image->getImagePage();
return $size['height'];
}
// 设置图像类型, 默认与源类型一致
public function set_type($type = 'png') {
$this->type = $type;
$this->image->setImageFormat($type);
}
// 获取源图像类型
public function get_type() {
return $this->type;
}
// 当前对象是否为图片
public function is_image() {
if ($this->image)
return true;
else
return false;
}
public function thumbnail($width = 100, $height = 100, $fit = true) {
$this->image->thumbnailImage($width, $height, $fit);
} // 生成缩略图 $fit为真时将保持比例并在安全框 $width X $height 内生成缩略图片
/*
添加一个边框
$width: 左右边框宽度
$height: 上下边框宽度
$color: 颜色: RGB 颜色 'rgb(255,0,0)' 或 16进制颜色 '#FF0000' 或颜色单词 'white'/'red'...
*/
public function border($width, $height, $color = 'rgb(220, 220, 220)') {
$color = new ImagickPixel();
$color->setColor($color);
$this->image->borderImage($color, $width, $height);
}
public function blur($radius, $sigma) {
$this->image->blurImage($radius, $sigma);
} // 模糊
public function gaussian_blur($radius, $sigma) {
$this->image->gaussianBlurImage($radius, $sigma);
} // 高斯模糊
public function motion_blur($radius, $sigma, $angle) {
$this->image->motionBlurImage($radius, $sigma, $angle);
} // 运动模糊
public function radial_blur($radius) {
$this->image->radialBlurImage($radius);
} // 径向模糊
public function add_noise($type = null) {
$this->image->addNoiseImage($type == null ? imagick::NOISE_IMPULSE : $type);
} // 添加噪点
public function level($black_point, $gamma, $white_point) {
$this->image->levelImage($black_point, $gamma, $white_point);
} // 调整色阶
public function modulate($brightness, $saturation, $hue) {
$this->image->modulateImage($brightness, $saturation, $hue);
} // 调整亮度、饱和度、色调
public function charcoal($radius, $sigma) {
$this->image->charcoalImage($radius, $sigma);
} // 素描
public function oil_paint($radius) {
$this->image->oilPaintImage($radius);
} // 油画效果
public function flop() {
$this->image->flopImage();
} // 水平翻转
public function flip() {
$this->image->flipImage();
} // 垂直翻转
/**
* 压缩图片 by jimmy 2014-4-11
* ----------------------------------------
*/
function compress($quality=100){
$this->image->setImageFormat('JPEG');
$this->image->setImageCompression(Imagick::COMPRESSION_JPEG);
$q = $this->image->getImageCompressionQuality();
$q = $q* $quality/100;
if($q==0){
$q = $quality;
}
$this->image->setImageCompressionQuality($q);
$this->image->stripImage();
}
/**
* 综合各个步骤输出 by jimmy 2014-4-11
* ----------------------------------------
*/
function output_image($srcFile,$destFile,$w=0,$h=0,$quality=100,$waterFile='',$fit="scale_fill"){
$this->open($srcFile);
$this->compress($quality);
if(!empty($waterFile)){
$this->add_watermark($waterFile);
}
if($w+$h>0){
$this->resize_to($w,$h,$fit);
}
$this->save_to($destFile);
}
}
First, from your comment "even quality =80 ,the l.jpg is more bigger than jm3.jpg" I think that is wrong. Re-saving the file with lower quality gives the following results.
119,300 = original image
266,008 = Resized at 100%
245,145 = Resized at 100% + despeckle
97,536 = Resized at 90% quality
88,342 = Resized at 90% quality with despeckle
I also added Imagick::despeckle as resizing can introduce speckles.
However the fundamental question you're asking it "Why does re-saving an JPEG image increase the file size, for the same quality?"
The simple answer is that after because it is a lossy format, after saving an image as a JPEG there can be more information in the new image. To save the image at the same quality, that additional information needs encoded, which takes up additional space.
Consider a simple sine wave; it contains very little information. Then imagine that we 'save it as an image' with a lossy format, which trims the ends off the sine wave i.e. something like:
Whichever way you consider the new shape it has more information that needs to be encoded to retain the new shape, so re-saving it at 100% quality takes a lot more space that the original lossy saved image.
Re-saving it at a lower quality produces a smaller image, but rapidly loses information from the image, as each saving is an approximation of an approximation.
TL:DR use PNG for all intermediate formats, and only use JPG for the final output to customers.

PHP: Resize/making thumbnails?

Helo i now have finish making my upload profilephoto system. Now i want include creating thumbnails of the uploaded image in different sizes eg 48x48 and 148x50, how can i do this?
Example / good tutorials for this?
You will need to use PHP's GD library or ImageMagick library.
First find out which, if any, you have installed on your development and production environments.
Then start looking for tutorials depending on which one you want to use. There are many out there.
GD usually comes pre-packed with PHP5.
Back then I used imagemagick.
$ convert -resize 48x48 xyz.jpg xyz_48x48.jpg
It is also available as a php module: http://php.net/manual/en/book.imagick.php
But I haven't used that one, but I suppose it knows exactly the same as the command line variant.
Tutorial with imagecreatefrom()... , imagecopyresized(), imagejpeg()
Here my class for resizing. Replace the 150 occurences with a variable.
<?php
/**
* Takes an image and creates a thumbnail of it.
*/
class ImageThumbnail
{
private $thumbnail;
/**
* Create a new object
*
* #param string $source Location of the original image (can be null if set using create())
* #param string $extension File extension, if it has been obfuscated (e.g. moved to PHP's tmp dir)
*/
public function __construct($source, $extension)
{
if ($source)
{
$this->create($source, $extension);
}
}
public function create($source, $extension = null)
{
if (!$extension)
{
$parts = explode('.', $source); // get the file extension
$extension = array_pop($parts);
}
else
{
$extension = ltrim($extension, '.'); // get rid of any prefixing dots
}
// Get the images size
$size = getImageSize($source);
// Width > height
if ($size[0] > $size[1])
{
$width = 150;
$height = (int) (150 * $size[1] / $size[0]);
}
// Height > width
else
{
$width = (int) (150 * $size[0] / $size[1]);
$height = 150;
}
$readFunc = 'imageCreateFrom'.filenameToImageMime($source);
if (!$source = $readFunc($source))
{
throw new Exception('The source image is unreadable');
}
// Create an blank image for the thumbnail
$thumbnail = imageCreateTrueColor($width, $height);
// Copy source image onto new image
imageCopyResized($thumbnail, $source, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
$this->thumbnail = $thumbnail;
}
public function getThumbnail()
{
return $this->thumbnail;
}
public function move($target)
{
$func = 'image'.filenameToImageMime($target);
$func($this->thumbnail, $target);
imageDestroy($this->thumbnail);
}
}
function makenicepic($srcfile,$tofile,$maxwidth,$maxheight) {
//global $_SGLOBAL;
// check file exist
if (!file_exists($srcfile)) {
return '';
}
$dstfile = $tofile;
include_once(S_ROOT.'./data/data_setting.php');
// //size
$tow = $maxwidth;
$toh =$maxheight;
$make_max = 0;
$maxtow = 950;
$maxtoh = 700;
$make_max = 1;
$im = '';
if($data = getimagesize($srcfile)) {
if($data[2] == 1) {
$make_max = 0;//gif skip
if(function_exists("imagecreatefromgif")) {
$im = imagecreatefromgif($srcfile);
}
} elseif($data[2] == 2) {
if(function_exists("imagecreatefromjpeg")) {
$im = imagecreatefromjpeg($srcfile);
}
} elseif($data[2] == 3) {
if(function_exists("imagecreatefrompng")) {
$im = imagecreatefrompng($srcfile);
}
}
}
if(!$im) return '';
$srcw = imagesx($im);
$srch = imagesy($im);
$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh){
$ftow = $tow;
$ftoh = $ftow*($srch/$srcw);
$fmaxtow = $maxtow;
$fmaxtoh = $fmaxtow*($srch/$srcw);
} else {
$ftoh = $toh;
$ftow = $ftoh*($srcw/$srch);
$fmaxtoh = $maxtoh;
$fmaxtow = $fmaxtoh*($srcw/$srch);
}
if($srcw <= $maxtow && $srch <= $maxtoh) {
$make_max = 0;
}
if($srcw > $tow || $srch > $toh) {
if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && #$ni = imagecreatetruecolor($ftow, $ftoh)) {
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
if($make_max && #$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh)) {
imagecopyresampled($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && #$ni = imagecreate($ftow, $ftoh)) {
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
if($make_max && #$maxni = imagecreate($fmaxtow, $fmaxtoh)) {
imagecopyresized($maxni, $im, 0, 0, 0, 0, $fmaxtow, $fmaxtoh, $srcw, $srch);
}
} else {
return '';
}
if(function_exists('imagejpeg')) {
imagejpeg($ni, $dstfile);
//big pic
if($make_max) {
imagejpeg($maxni, $srcfile);
}
} elseif(function_exists('imagepng')) {
imagepng($ni, $dstfile);
if($make_max) {
imagepng($maxni, $srcfile);
}
}
imagedestroy($ni);
if($make_max) {
imagedestroy($maxni);
}
}
imagedestroy($im);
if(!file_exists($dstfile)) {
return '';
} else {
return $dstfile;
}
}

CAn't resize image with GDlib2

I'm trying to resize an image of more than 3000px in width, it uploads fine if i do not resize it, but as soon as I resize the image it does not want to upload and resize, this is funny and I can't seem to understnd, since if the width of the image is alot smaller everthing works just fine. Are there limitations to this?
Try this... Not Using GD but i think it will work for u.
<?php
function Image($image, $crop = null, $size = null) {
$image = ImageCreateFromString(file_get_contents($image));
if (is_resource($image) === true) {
$x = 0;
$y = 0;
$width = imagesx($image);
$height = imagesy($image);
/*
CROP (Aspect Ratio) Section
*/
if (is_null($crop) === true) {
$crop = array($width, $height);
} else {
$crop = array_filter(explode(':', $crop));
if (empty($crop) === true) {
$crop = array($width, $height);
} else {
if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false)) {
$crop[0] = $crop[1];
} else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false)) {
$crop[1] = $crop[0];
}
}
$ratio = array(0 => $width / $height, 1 => $crop[0] / $crop[1]);
if ($ratio[0] > $ratio[1]) {
$width = $height * $ratio[1];
$x = (imagesx($image) - $width) / 2;
}
else if ($ratio[0] < $ratio[1]) {
$height = $width / $ratio[1];
$y = (imagesy($image) - $height) / 2;
}
}
/*
Resize Section
*/
if (is_null($size) === true) {
$size = array($width, $height);
}
else {
$size = array_filter(explode('x', $size));
if (empty($size) === true) {
$size = array(imagesx($image), imagesy($image));
} else {
if ((empty($size[0]) === true) || (is_numeric($size[0]) === false)) {
$size[0] = round($size[1] * $width / $height);
} else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false)) {
$size[1] = round($size[0] * $height / $width);
}
}
}
$result = ImageCreateTrueColor($size[0], $size[1]);
if (is_resource($result) === true) {
ImageSaveAlpha($result, true);
ImageAlphaBlending($result, true);
ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255));
ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height);
ImageInterlace($result, true);
ImageJPEG($result, null, 90);
}
}
return false;
}
header('Content-Type: image/jpeg');
Image('image.jpg', '2:1', '1200x');
?>

Categories