The image functions is the easiest create resize image using PHP. You can customize the image width and height.
The image functions are used to create resize image with PHP:
- getimagesize ( ):
Get image original width and height
- imagecreatetruecolor( ):
It returns an image specified size
- imagecopyresampled( ):
Copy and resize height and width of image with resampling
- imagecreatefrompng( ), imagecreatefromgif( ) and imagecreatefromjpeg( ):
Returns an image identifier representing the image obtained from the given filename
- imagepng( ): Creates a PNG image
- imagegif( ): Creates a GIF image
- imagejpeg( ): Creates a JPEG image
<?php
if (is_array($_FILES)) {
$file = $_FILES['image']['tmp_name'];
$sourceProperties = getimagesize($file);
$fileNewName = ($lastID + 1);
$folderPath = "../Images/Slider-Images/";
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$imageType = $sourceProperties[2];
$imgefolder = ($lastID + 1) . "." . $ext;
switch ($imageType) {
case IMAGETYPE_PNG:
$imageResourceId = imagecreatefrompng($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagepng ($targetLayer, $folderPath . $fileNewName. "." . $ext);
break;
case IMAGETYPE_GIF:
$imageResourceId = imagecreatefromgif($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagegif ($targetLayer, $folderPath . $fileNewName. "." . $ext);
break;
case IMAGETYPE_JPEG:
$imageResourceId = imagecreatefromjpeg($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagejpeg ($targetLayer, $folderPath . $fileNewName. "." . $ext);
break;
case IMAGETYPE_JPG:
$imageResourceId = imagecreatefromjpeg($file);
$targetLayer = imageResize($imageResourceId, $sourceProperties[0], $sourceProperties[1]);
imagejpeg ($targetLayer, $folderPath . $fileNewName. "." . $ext);
break;
default:
echo "Invalid Image type.";
exit;
break;
}
move_uploaded_file($imgefolder, $folderPath );
$_POST['image'] = $imgefolder;
}
$sql = "INSERT INTO gallary_list (image) VALUES ('" . $_POST [‘image’]. "')";
mysqli_query($con, $sql);
?>
We can use the function imageResize ( )
function imageResize($imageResourceId, $width, $height) {
$targetWidth = 1550;
$targetHeight = 600;
$targetLayer = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetLayer, $imageResourceId, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height);
return $targetLayer;
}