I’m using this php class (http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php) for creating the different sized images that my website requires.
I am using the resizeToWidth() method.
Everything is great except for if I’m requesting an output-width greater than the original, then the image is upscaled and pixelated to meet the dimensions.
What’s the best way of,
if the image is too small,
keeping the original image and padding out the background/cavas with white space to meet required width?
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);}
calls:
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image; }
I’m thinking i need:
- An if statement in the resizeToWidthStatement
- an extra parameter ie ‘pad=true’ in the resize() method
- something in the resize() method that centres the image and adds the whitespace
any ideas?
You’re on the right track. Put a conditional check into the resizeToWidth to check if you’re resizing up.
In the resize method, if you want to pad, then first create a new image of the new, larger dimensions, then figure out the center point
( ( new_image_width - old_image_width ) / 2 )and to animagecopyto that new location.