PHP Snippets
resize
Information
(PHP Version >= 4.0.6)Functions used in this snippet: file_exists, substr, strrpos, stristr, imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng, imageSX, imageSY, ceil, imageCreatetruecolor, imagecreatetrueColor, imagecopyresized, isset, strtolower, imagecopy, imagedestroy.
Description
int resize( str image, int x [, int y [, str wm [, str wml]]])
Uses image to resize based on the size specification of x and y. If y is set to NULL the image will be resized to have its largest dimension be x pixels high/wide.
If wm is specified, it will be used as a watermark image to be placed over the resized image in the region wml. Where the first character places the watermark in the t - Top or b - Bottom and the second character places the watermark on the l - Left or r - Right side of the image.
Snippet
<?php
function resize($image,$x,$y=NULL,$wm=NULL,$wml='br'){
if(!file_exists($image)){
return false;
}
$images = array();
if($wm !== '' && $wm !== NULL && file_exists($wm)){
$images['wmimg'] = $wm;
}
$images['img'] = $image;
foreach($images as $key=>$value){
$type = substr($value,strrpos($value,'.'));
if(stristr($type,'i')){
$$key = imagecreatefromgif($value);
}
if(stristr($type,'j')){
$$key = imagecreatefromjpeg($value);
}
if(stristr($type,'n')){
$$key = imagecreatefrompng($value);
}
}
$size = array();
if($y === '' || $y === NULL){
$size['x'] = imageSX($img);
$size['y'] = imageSY($img);
if($size['x'] >= $size['y']){
$size['dest_x'] = $x;
$size['dest_y'] = ceil($size['y'] * ($x / $size['x']));
}else{
$size['dest_y'] = $x;
$size['dest_x'] = ceil($size['x'] * ($x / $size['y']));
}
$dest = imageCreatetruecolor($size['dest_x'],$size['dest_y']);
}else{
$dest = imagecreatetrueColor($x,$y);
$size['x'] = imageSX($img);
$size['y'] = imageSY($img);
$size['dest_x'] = $x;
$size['dest_y'] = $y;
}
imagecopyresized($dest, $img, 0, 0, 0, 0, $size['dest_x'], $size['dest_y'], $size['x'], $size['y']);
if(isset($wmimg)){
$size['wmx'] = imageSX($wmimg);
$size['wmy'] = imageSY($wmimg);
$size['wmh'] = strtolower($wml{0}) === 'b' ? ($size['dest_y'] - $size['wmy'] - 2) : 2;
$size['wmw'] = strtolower($wml{1}) === 'r' ? ($size['dest_x'] - $size['wmx'] - 2) : 2;
imagecopy($dest, $wmimg, $size['wmw'], $size['wmh'], 0, 0, $size['wmx'], $size['wmy']);
imagedestroy($wmimg);
}
imagedestroy($img);
return $dest;
}
?>
Example
resize Can be used in the following way:
<?php
header('Content-type: image/jpeg'); // set your header for jpeg image
imagejpeg(resize('imagefile.png',100));
// outputs imagefile.png as a jpeg constrained to 100 px with its ratio kept the same
header('Content-type: image/png'); // set your header for png image
imagepng(resize('filename.png',100,100));
// outputs filename.png as a png and as a 100X100 image
header('Content-type: image/png'); // set your header for png image
imagepng(resize('imagefile.jpg',100,NULL,'logo.png','bl'));
// outputs imagefile.jpg as a png constrained to 100 px with its ratio kept the same
// it also adds logo.png on top of the image on the bottom left of the image
?>
Updated on Jun 27th at 5 pm.
1 Response to resize
Just a quick note, I usually have the function pass a GD image resource and this one you just need to pass the filename. Just a reminder.