How to resize an image in PHP?
This can use when you create thumbnail images.
In this php code current date time is used to save the resized image.
You may need to create a directory named "images" in your application and another directory in it called "resized"
to save resized images.
Functions that are needed to resize an image using PHP
PHP function | Purpose |
---|---|
file_exists ( string $filename ) | Checks whether a file exists. |
getimagesize ( string $filename [, array &$imageinfo ] ) | determine the size of any given image file. |
imagecreatefromgif ( string $filename ) | Create an image identifier using the image obtained from the given filename. |
imagecreatefromjpeg ( string $filename ) | Create an image identifier using the image obtained from the given filename. |
imagecreatefrompng ( string $filename ) | Create an image identifier using the image obtained from the given filename. |
imagesx ( resource $image ) | Get the width of the given image resource. |
imagesy ( resource $image ) | Get the height of the given image resource. |
imagecreatetruecolor ( int $width , int $height ) | Create a new true color image of the specified size. |
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) | Copies a rectangular portion of one image to another image |
imagegif ( resource $image [, string $filename ] ) | Creates the GIF file in filename from the image image. |
imagejpeg ( resource $image [, string $filename [, int $quality ]] ) | Creates a JPEG file from the given image. |
imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] ) | Outputs or saves a PNG image from the given image . |
1. Example code for resize an image using PHP.
PHP Codeprivate $imageType;
private $imageHeight;
private $imageWidth;
function loadImageFile($image_path){
if(file_exists($image_path)){
$image_info=getimagesize($image_path);
switch(strtolower($image_info['mime'])){
case "image/gif":{
$this->imageType=IMAGETYPE_GIF;
$this->image=imagecreatefromgif($image_path);
}break;
case "image/jpeg":{
$this->imageType=IMAGETYPE_JPEG;
$this->image=imagecreatefromjpeg($image_path);
}break;
case "image/png":{
$this->imageType=IMAGETYPE_PNG;
$this->image=imagecreatefrompng($image_path);
}break;
default:{
die("Incompatible file type");
}
}
$this->imageWidth=imagesx($this->image);
$this->imageHeight=imagesy($this->image);
}else{
die("File does not exists");
}
}
function resizeImage($width,$height,$resizedImageName=''){
$resizedImage=imagecreatetruecolor($width,$height);
imagecopyresampled($resizedImage,$this->image,0,0,0,0,$width,$height,$this->imageWidth,$this->imageHeight);
$this->image=$resizedImage;
if($resizedImageName==''){
$resizedImageName="images/resized/".date('Y')."-".date('m')."-".date('d')."-".date('H')."-".date('i')."-".date('s');
}
switch($this->imageType){
case 1:{//gif
imagegif($this->image,$resizedImageName.".gif");
}break;
case 2:{//jpg
imagejpeg($this->image,$resizedImageName.".jpg",75);
}break;
case 3:{//png
imagepng($this->image,$resizedImageName.".png");
}break;
}
}
}
?>
$resize=new ImageResize();
$image_path="images/Image1.jpg";
$resize->loadImageFile($image_path);
$resize->resizeImage(80,80);