How Can i resize a batch picture in Php Is their any command .If so Then Kindly tell me
thanks in advance
Printable View
How Can i resize a batch picture in Php Is their any command .If so Then Kindly tell me
thanks in advance
You can use imagecopyresized if you have GD installed. If you are using ImageMagick, use ResampleImage or one of its variants.
Remember to take into consideration, if you are using this for image upload and you are allowing multiple formats, that you need to use different imagecreatefrom____()
EDIT:
here is a script i used for image resizing. Note that this doesn't save the image. it is for resizing on the flash
PHP Code:<?php
session_start();
$mem_id = $_SESSION['member_id'];
# Constants
define(IMAGE_BASE, '../pics/');
define(IMAGE_BASE1, '../pics/');
define(MAX_WIDTH, 90);
define(MAX_HEIGHT, 90);
# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE.$image_file;
//print $image_path;
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
}
else if ($ext == 'gif') {
$img = @imagecreatefromgif($image_path);
# Only if your version of GD includes GIF support
}
else if ($ext == 'bmp') {
$img = imagecreatefrombmp($image_path);
# Only if your version of GD includes GIF support
}
else if ($ext == 'tif') {
$img = imagecreatefromtif($image_path);
# Only if your version of GD includes GIF support
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
$image_path = IMAGE_BASE1."nophoto.jpg";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
}
# Display the image
if($img)
{
header("Content-type: image/jpeg");
imagejpeg($img);
}
else
{
print "$image_path";
}
?>