Jump to content

resize (edit) of picture


anatak

Recommended Posts

Does anybody know if it is possible to resize and save the resized version of a picture with php ?

I am not talking about resizing a picture (that keeps the same filesize) but editing the picture like making a thumbnail from a full size picture.

 

When I look for resizing with php I always get scripts that do not save the smaller picture.

 

kind regards

anatak

Link to comment
Share on other sites

I actually made a class that does exactly this. It's pretty good for image manipulation. It could have more, but for my purposes it's more than enough.

 

<?php
class Image
{
var $image, $ext, $file;
public function __construct($im=NULL)
{
	define('CENTER', 0, true);
	define('CENTER_LEFT', 1, true);
	define('CENTER_RIGHT', 2, true);
	define('TOP_CENTER', 3, true);
	define('TOP_LEFT', 4, true);
	define('TOP_RIGHT', 5, true);
	define('BOTTOM_CENTER', 6, true);
	define('BOTTOM_LEFT', 7, true);
	define('BOTTOM_RIGHT', 8, true);

	if(empty($im)) return true;
	$info = @getimagesize($im);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function setImage($im)
{
	$info = @getimagesize($this->file);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($porportional)
	{
		if(empty($max) && empty($percent)) return false;
		if(empty($max))
		{
			$new_width = $info[0] * ($percent/100);
			$new_height = $info[1] * ($percent/100);
		}
		else
		{
			if($info[0] < $max && $info[1] < $max) return false;
			$new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max;
			$new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max;
		}
	}
	else
	{
		$new_width = $width;
		$new_height = $height;
	}
	$new_image = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
	$this->image = $new_image;
	return true;
}
public function crop($width, $height, $x, $y, $position=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($width > $info[0] || $height > $info[1]) return false;
	if(!empty($position) && $position >= 0 && $position <= 
	{
		switch($position)
		{
			case CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_LEFT:
				$x = 0;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_RIGHT:
				$x = ($info[0] - $height);
				$y = ($info[1] - $height)/2;
			break;
			case TOP_CENTER:
				$x = ($info[0] - $width)/2;
				$y = 0;
			break;
			case TOP_LEFT:
				$x = 0;
				$y = 0;
			break;
			case TOP_RIGHT:
				$x = ($info[0] - $width);
				$y = 0;
			break;
			case BOTTOM_CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_LEFT:
				$x = 0;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_RIGHT:
				$x = ($info[0] - $width);
				$y = ($info[1] - $height);
			break;
			default:
				return false;
			break;
		}
	}
	$new_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
	$this->image = $new_image;
	return true;
}
public function save($loc, $compression=0)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$func = 'image' . $this->ext;
	return $func($this->image, $loc, $compression);
}
public function output()
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	header('Content-type: image/' . $this->ext);
	$func = 'image' . $this->ext;
	$func($this->image);
}
}

 

For your case you could do something like..

 

<?php
require_once 'image.class.php'; //My Class
$im = new Image('/path/to/image');
$im->resize(width, height);
//Or if you want to do it by a % proportionally..
$im->resize(null, null, true, %);
$im->size('someplace.png/jpg/gif');

 

There's also a lot of other options included in the class. For example you can crop (which also offers different location, CENTER_LEFT, CENTER_RIGHT, .. 9 in total).

 

Here's the class with some documentation/examples I wrote for it:

 

http://pastesite.com/10199

Link to comment
Share on other sites

class Image {
    var $image, $ext, $file;
    public function __construct($im=NULL)
        define('CENTER', 0, true);
        define('CENTER_LEFT', 1, true);
        define('CENTER_RIGHT', 2, true);
        define('TOP_CENTER', 3, true);
        define('TOP_LEFT', 4, true);
        define('TOP_RIGHT', 5, true);
        define('BOTTOM_CENTER', 6, true);
        define('BOTTOM_LEFT', 7, true);
        define('BOTTOM_RIGHT', 8, true);

 

Alex couldn't decide if you would write for PHP 4 or 5? ;)

 

class Image {
    const CENTER = 0;
    ..
    
    private $image;
    private $ext;
    private $file;
    
    public function __construct($im = null)
}

Link to comment
Share on other sites

Btw, you should probably use this:

 

class Image
{
private $image, $ext, $file;

const CENTER = 0, CENTER_LEFT = 1, CENTER_RIGHT = 2, TOP_CENTER = 3, TOP_LEFT = 4, TOP_RIGHT = 5, BOTTOM_CENTER = 6, BOTTOM_LEFT = 7, BOTTOM_RIGHT = 8;

public function __construct($im=NULL)
{	
	if(empty($im)) return true;
	$info = @getimagesize($im);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function setImage($im)
{
	$info = @getimagesize($this->file);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($porportional)
	{
		if(empty($max) && empty($percent)) return false;
		if(empty($max))
		{
			$new_width = $info[0] * ($percent/100);
			$new_height = $info[1] * ($percent/100);
		}
		else
		{
			if($info[0] < $max && $info[1] < $max) return false;
			$new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max;
			$new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max;
		}
	}
	else
	{
		$new_width = $width;
		$new_height = $height;
	}
	$new_image = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
	$this->image = $new_image;
	return true;
}
public function crop($width, $height, $x, $y, $position=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($width > $info[0] || $height > $info[1]) return false;
	if(!empty($position) && $position >= 0 && $position <= 
	{
		switch($position)
		{
			case CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_LEFT:
				$x = 0;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_RIGHT:
				$x = ($info[0] - $height);
				$y = ($info[1] - $height)/2;
			break;
			case TOP_CENTER:
				$x = ($info[0] - $width)/2;
				$y = 0;
			break;
			case TOP_LEFT:
				$x = 0;
				$y = 0;
			break;
			case TOP_RIGHT:
				$x = ($info[0] - $width);
				$y = 0;
			break;
			case BOTTOM_CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_LEFT:
				$x = 0;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_RIGHT:
				$x = ($info[0] - $width);
				$y = ($info[1] - $height);
			break;
			default:
				return false;
			break;
		}
	}
	$new_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
	$this->image = $new_image;
	return true;
}
public function save($loc, $compression=100)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$func = 'image' . $this->ext;
	return $func($this->image, $loc, $compression);
}
public function output()
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	header('Content-type: image/' . $this->ext);
	$func = 'image' . $this->ext;
	$func($this->image);
}
}

 

It just reflects the changes and fixes the problem(s) that igance pointed out.

Link to comment
Share on other sites

I changed the original to include a resize function that keeps the aspect ratio.

Maybe it was already possible with your code but I will gladly admit that I am not as good a programmer as you are and I did not fully understand your code.

 

Here is what I use

I will use the updated version.

<?php
class Image
{
var $image, $ext, $file;
public function __construct($im=NULL)
{
	define('CENTER', 0, true);
	define('CENTER_LEFT', 1, true);
	define('CENTER_RIGHT', 2, true);
	define('TOP_CENTER', 3, true);
	define('TOP_LEFT', 4, true);
	define('TOP_RIGHT', 5, true);
	define('BOTTOM_CENTER', 6, true);
	define('BOTTOM_LEFT', 7, true);
	define('BOTTOM_RIGHT', 8, true);

	if(empty($im)) return true;
	$info = @getimagesize($im);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function setImage($im)
{
	$info = @getimagesize($this->file);
	$backtrace = debug_backtrace();
	if(!$info)
		return die('<b>Fatal error:</b> \'' . $im . '\' is not a valid image resource on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$ex = explode('/', $info['mime']);
	$this->ext = $ex[1];
	$func = 'imagecreatefrom' . $this->ext;
	$this->image = @$func($im);
	$this->file = $im;
	return true;
}
public function aspect_resize($max_width, $max_height)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	//get the width and height of the image
	$info = Array(imagesx($this->image), imagesy($this->image));
	//check if image width height is greater than max_width max_height
	if($info[0]>$max_width OR $info[1]>$max_height){
		//check wich value is greater, width or height
		if($info[0]>$info[1]){
			//landscape: width is greater than height
			//get the aspect ratio of the picture (greatest value divided by smallest value)
			$aspect=$info[0]/$info[1];
			//image resize if width is greatest value new_width=$desire_width, new_height = desire_width/aspect ratio
			$new_width = $max_width;
			$new_height = $max_width/$aspect;
		}else{
			//portrait or square
			//get the aspect ratio of the picture (greatest value divided by smallest value)
			$aspect=$info[1]/$info[0];
			//image resize if height is greatest value new_height=$desire_heigth, new_width = desire_heightth/aspect ratio
			$new_height = $max_height;
			$new_widt = $max_height/$aspect;
		}
		$new_image = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
		$this->image = $new_image;
		return true;
	}
}

public function resize($width, $height, $porportional=FALSE, $percent=NULL, $max=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->resize, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($porportional)
	{
		if(empty($max) && empty($percent)) return false;
		if(empty($max))
		{
			$new_width = $info[0] * ($percent/100);
			$new_height = $info[1] * ($percent/100);
		}
		else
		{
			if($info[0] < $max && $info[1] < $max) return false;
			$new_width = ($info[0] > $info[1]) ? $max : ($info[0]/$info[1]) * $max;
			$new_height = ($info[0] > $info[1]) ? ($info[1]/$info[0]) * $max : $max;
		}
	}
	else
	{
		$new_width = $width;
		$new_height = $height;
	}
	$new_image = imagecreatetruecolor($new_width, $new_height);
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
	$this->image = $new_image;
	return true;
}

public function crop($width, $height, $x, $y, $position=NULL)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->crop, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$info = Array(imagesx($this->image), imagesy($this->image));
	if($width > $info[0] || $height > $info[1]) return false;
	if(!empty($position) && $position >= 0 && $position <= 
	{
		switch($position)
		{
			case CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_LEFT:
				$x = 0;
				$y = ($info[1] - $height)/2;
			break;
			case CENTER_RIGHT:
				$x = ($info[0] - $height);
				$y = ($info[1] - $height)/2;
			break;
			case TOP_CENTER:
				$x = ($info[0] - $width)/2;
				$y = 0;
			break;
			case TOP_LEFT:
				$x = 0;
				$y = 0;
			break;
			case TOP_RIGHT:
				$x = ($info[0] - $width);
				$y = 0;
			break;
			case BOTTOM_CENTER:
				$x = ($info[0] - $width)/2;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_LEFT:
				$x = 0;
				$y = ($info[1] - $height);
			break;
			case BOTTOM_RIGHT:
				$x = ($info[0] - $width);
				$y = ($info[1] - $height);
			break;
			default:
				return false;
			break;
		}
	}
	$new_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($new_image, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
	$this->image = $new_image;
	return true;
}
public function save($loc, $compression=0)
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->save, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	$func = 'image' . $this->ext;
	return $func($this->image, $loc, $compression);
}
public function output()
{
	$backtrace = debug_backtrace();
	if(empty($this->image)) return die('<b>Fatal error:</b> Invalid call to Image()->output, no image is set on <b>line ' . $backtrace[0]['line'] . ' [' . $backtrace[0]['file'] . ']</b>');
	header('Content-type: image/' . $this->ext);
	$func = 'image' . $this->ext;
	$func($this->image);
}
}
?>

 

I just added

public function aspect_resize($max_width, $max_height)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.