Resize Image While Uploading
i want to Resize Image While Uploading 400*400 px
i saw that code on a website
don't know how to do that :(
plz help
PHP Code:
/**
* Image resize
* @param int $width
* @param int $height
*/
function resize($width, $height){
/* Get original image x y*/
list($w, $h) = getimagesize($_FILES['image']['tmp_name']);
/* calculate new image size with ratio */
$ratio = max($width/$w, $height/$h);
$h = ceil($height / $ratio);
$x = ($w - $width / $ratio) / 2;
$w = ceil($width / $ratio);
/* new file name */
$path = 'uploads/'.$width.'x'.$height.'_'.$_FILES['image']['name'];
/* read binary data from image file */
$imgString = file_get_contents($_FILES['image']['tmp_name']);
/* create image from string */
$image = imagecreatefromstring($imgString);
$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $image,
0, 0,
$x, 0,
$width, $height,
$w, $h);
/* Save image */
switch ($_FILES['image']['type']) {
case 'image/jpeg':
imagejpeg($tmp, $path, 100);
break;
case 'image/png':
imagepng($tmp, $path, 0);
break;
case 'image/gif':
imagegif($tmp, $path);
break;
default:
exit;
break;
}
return $path;
/* cleanup memory */
imagedestroy($image);
imagedestroy($tmp);
}
Re: Resize Image While Uploading
What is it exactly you're looking to do, is it for your product images? Have you tried the image handler mod?
Re: Resize Image While Uploading
Quote:
Originally Posted by
picandnix
What is it exactly you're looking to do, is it for your product images? Have you tried the image handler mod?
look, i need to add many product regularly and condition is all product should 400*400 px
so i want when i add a new product whatever size i upload i will be automatic convert 400*400 px
i installed image handler its not do it automatically.
Re: Resize Image While Uploading
Quote:
Originally Posted by
jennydutch
look, i need to add many product regularly and condition is all product should 400*400 px
so i want when i add a new product whatever size i upload i will be automatic convert 400*400 px
i installed image handler its not do it automatically.
I use this for all my sites and images, works perfectly, and fast. You can set image size and optimize all in one swoop.
Re: Resize Image While Uploading
Quote:
Originally Posted by
picandnix
I use
this for all my sites and images, works perfectly, and fast. You can set image size and optimize all in one swoop.
Thanks going to download :)
Re: Resize Image While Uploading
Quote:
Originally Posted by
jennydutch
Thanks going to download :)
Let me know how you get with it.
Re: Resize Image While Uploading
Quote:
Originally Posted by
picandnix
Let me know how you get with it.
right now i am doing this manually
Re: Resize Image While Uploading
Quote:
Originally Posted by
jennydutch
i installed image handler its not do it automatically.
it most certainly does do the resizing automatically based on your admin image settings.. HOWEVER, your SOURCE image must be equal dimensions if you expect it to be resized in equal dimensions. So if you have a 1024 x 1024 image and you set your large image settings to 400 x 400, then IH4 most DEFINITELY will resize the image as you expect.. However if your source image is 1024 x 800 IH will resize PROPORTIONALLY based on your image size settings.