
Website load time is now a days a key aspect of website designing and development.Images, which play a major role in designing should be nearly the same as of their usable size. For instance to show a small thumbnail of say 50px its not good to have an image of size greater than 200px and reduce its size directly in the image tag. In this way it will load the big image from server and will unnecessarily take longer time. So here i will like to share with you a simple image resize script in php which will reduce the size (keeping resolution same) and will even allow to choose a new compression factor.
png images which have transparent backgrounds tends to look hazy and disordered when tried to resize and all we get is a black background instead of the transparent one. So this critical issue is also taken care off in the following script .
The only requirement is that you must have gd version 2.0 (or greater) installed on your server.
<?php
$img_des = resize_img($picture_location,'new width');
if(preg_match(".jpg",picture_location) || preg_match(".jpeg",picture_location))
imagejpeg($img_des,picture_location,'100'); // 100 refers to the highest level of accuracy(no compression)
elseif(preg_match(".png",picture_location))
imagepng($img_des,picture_location,'0'); // for png's the compression ratio is from 0 (no compression) to 9
elseif(preg_match(".gif",$picture_location))
imagegif($img_des,picture_location,'100');
?>
resize_img is the function which takes original uploaded picture location and new desired width as parameters.
Below is the code for the resize_img function:
<? php
function resize_img($imgname,$size,$thumb="0") {
if(preg_match(".jpg",$imgname) || preg_match(".jpeg",$imgname)) $img_src = ImageCreateFromjpeg($imgname);
elseif(preg_match(".png",$imgname)) $img_src = ImageCreateFrompng($imgname);
elseif(preg_match(".gif",$imgname)) $img_src = ImageCreateFromgif($imgname);
$true_width = imagesx($img_src);
$true_height = imagesy($img_src);
$width=$size;
$height = ($width/$true_width)*$true_height;
$nheight = $height;
if("your gd version" >= 2) { //GD 2.0+
$img_des = imagecreatetruecolor($width,$nheight);
if(preg_match(".png",$imgname)) // used for managing transparency of pngs
{
imagesavealpha($img_des, true);
$trans_colour = imagecolorallocatealpha($img_des, 0, 0, 0, 127);
imagefill($img_des, 0, 0, $trans_colour);
$red = imagecolorallocate($img_des, 255, 0, 0);
imagefilledellipse($img_des, 400, 300, 400, 300, $red);
}
imagecopyresampled($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
}
else{ //GD < 2.0
$img_des = ImageCreate($width,$nheight);
imagecopyresized ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
}
return $img_des;
}
?>
so this script works for png,jpg,gif maintaining background transparency for pngs.
Thanks
April 23, 2013
April 3, 2013
Hi,
you can also resize image in php with imagemagick.
ImageMagick works better as GD, faster and images doesn’t lost quality.
Yahoo Flickr also uses image magick.
I am using this code and it made my life easier. i love the crop which is perfect. 1 question if some one upload big image then its fine but when some one upload small pic so crop function still work and make that small image to final size and picture distort . How can i tell script not to run when image is small than final size.