Type Here to Get Search Results !
×
Hi ,

We always try to serve you better. Please enter the institute name, you belong to.

×
Hello ,

Your daily download limit has been exhausted. Share it with your friends to download more.

How to resize an image by some pixel say 200px in PHP

To resize an image by 200px in PHP, you can use the GD library which is a popular PHP extension for manipulating images.

Here's an example code that resizes an image by 200px:


// Load the image from a file $filename = 'path/to/image.jpg'; $image = imagecreatefromjpeg($filename); // Get the current dimensions of the image $width = imagesx($image); $height = imagesy($image); // Calculate the new dimensions of the image $newWidth = $width + 200; $newHeight = $height + 200; // Create a new image with the new dimensions $newImage = imagecreatetruecolor($newWidth, $newHeight); // Copy and resize the original image to the new image imagecopyresized($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the new image to a file $newFilename = 'path/to/new_image.jpg'; imagejpeg($newImage, $newFilename); // Free up memory imagedestroy($image); imagedestroy($newImage);


This code loads an image from a file using imagecreatefromjpeg(), calculates the new dimensions of the image by adding 200 pixels to the width and height, creates a new image with the new dimensions using imagecreatetruecolor(), copies and resizes the original image to the new image using imagecopyresized(), saves the new image to a file using imagejpeg(), and frees up memory using imagedestroy().

Note that you can replace imagecreatefromjpeg() with imagecreatefrompng() or imagecreatefromgif() if you're working with a PNG or GIF image respectively.


Resize image to max 200px by width and height

To resize an image to a maximum of 200px by width and height in PHP, you can use the GD library which is a popular PHP extension for manipulating images.

Here's an example code that resizes an image to a maximum of 200px by width and height:

// Load the image from a file $filename = 'path/to/image.jpg'; $image = imagecreatefromjpeg($filename); // Get the current dimensions of the image $width = imagesx($image); $height = imagesy($image); // Calculate the new dimensions of the image $maxWidth = 200; $maxHeight = 200; if ($width > $maxWidth || $height > $maxHeight) { $scale = min($maxWidth / $width, $maxHeight / $height); $newWidth = round($width * $scale); $newHeight = round($height * $scale); } else { $newWidth = $width; $newHeight = $height; } // Create a new image with the new dimensions $newImage = imagecreatetruecolor($newWidth, $newHeight); // Copy and resize the original image to the new image imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); // Save the new image to a file $newFilename = 'path/to/new_image.jpg'; imagejpeg($newImage, $newFilename); // Free up memory imagedestroy($image); imagedestroy($newImage);


This code loads an image from a file using imagecreatefromjpeg(), calculates the new dimensions of the image by scaling the width and height down if either of them are larger than 200 pixels, creates a new image with the new dimensions using imagecreatetruecolor(), copies and resizes the original image to the new image using imagecopyresampled(), saves the new image to a file using imagejpeg(), and frees up memory using imagedestroy().

Note that you can replace imagecreatefromjpeg() with imagecreatefrompng() or imagecreatefromgif() if you're working with a PNG or GIF image respectively. Also note that imagecopyresampled() is used instead of imagecopyresized() to improve the quality of the resized image.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.