0

I want to auto resize the image while uploading.I have wondered many site but in every site users have put the new-width and new-height for all the images but i want auto resize becasue problem is when user will upload same dimension image then we will easily cut but when user upload landscape or portrait dimension image then image will messed and cut wrong dimensions. So i am facing this issue.

Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64
  • Possible duplicate of [PHP Thumbnail Image Resizing with proportions](http://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions?rq=1) (but with different `$maxwidth` and `$maxheight`) (*I would've cast a close vote, but I'm getting a javascript error: `Uncaught TypeError: Cannot read property 'closeSubmitting' of undefined`*) – h2ooooooo May 15 '14 at 07:13
  • How about showing some code to show where you are having problems? – Mike Brant May 15 '14 at 07:19

2 Answers2

0

You should use those tutorials, but calculate the other value yourself. For example you have an image with a width of 600px and a height of 1200px; and you want it to be 200px width. Calculate it like this:

$newImageWidth = 200;
$imageWidth = 600;
$imageHeight = 1200;
$ratio = $imageHeight / $imageWidth;
$newImageHeight = $newImageWidth * ratio;
Arko Elsenaar
  • 1,689
  • 3
  • 18
  • 33
  • Thanks you for reply but same issue again if image size is 2000*500 then again image will cut in wrong dimension. – Pritesh Mahajan May 15 '14 at 07:11
  • Why? It will resize to 200x50 in this case. What dimensions do you want to get? – Arko Elsenaar May 15 '14 at 07:12
  • If you want to show example so please check this link http://www.webappers.com/2008/03/14/smart-image-resizer-auto-resizes-images-in-php/ In this link one coffee image you are showing. i want just like that – Pritesh Mahajan May 15 '14 at 07:12
  • What do you mean? - If you have the first image as upload, you can put something like `$newImageWidth = 100;` in your code, and the rest should work. You should get the right height calculated. – Arko Elsenaar May 15 '14 at 07:14
  • Any Dimension like if user upload 2000*50 , 500*1000,200*200,1024*900 all dimension. Image will resize perfectly – Pritesh Mahajan May 15 '14 at 07:14
  • What output do you want to get? Like, should the image always have a width of 200px? Or what do you want? Please be more specific. – Arko Elsenaar May 15 '14 at 07:15
  • if user will upload 300 (width) * 700 (Height) image. – Pritesh Mahajan May 15 '14 at 07:22
  • I think you did not finish your sentence – Arko Elsenaar May 15 '14 at 07:23
  • @ArkoElsenaar Your script will calculate the new image dimensions to 200x400, not 200x50. –  May 15 '14 at 07:43
  • Yes, I noticed. I am waiting for the OP to explain what he *really* wants so I can modify my answer. – Arko Elsenaar May 15 '14 at 07:45
  • i just want to auto resize my image without put any height and widht. code will measure the image and cut it automatically with 100% resolution. if we put the new height or new width then image will create in same height or widht that we put. that is my concern – Pritesh Mahajan May 15 '14 at 07:59
  • @PriteshMahajan You need a "desired width" and a "desired height". You can't just `resize($image)` and expect it to know how you want it resized. If I had an image being `1200x800` pixels, *what* dimensions would it be resized to? `2400x1600`? `600x400`? `300x200`? `150x100`? `75x50`? – h2ooooooo May 15 '14 at 08:03
  • if we put the new height or width then image will stretched – Pritesh Mahajan May 15 '14 at 08:03
  • @PriteshMahajan Not when you use proportions like you do here. Did you even see the link I posted in your original question? All you'd have to do is fix the width and height. – h2ooooooo May 15 '14 at 08:04
0
In auto resize case this may be helpful.i am using this code in my oproject 

list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;


$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));

if ($ratio < 1) {
    $targetWidth = $targetHeight * $ratio;
} else {
    $targetHeight = $targetWidth / $ratio;
}

$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;


$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);

if ($ratio < 1) {
    $srcX = 0;
    $srcY = ($originalHeight / 2) - ($originalWidth / 2);
    $srcWidth = $srcHeight = $originalWidth;
} else {
    $srcY = 0;
    $srcX = ($originalWidth / 2) - ($originalHeight / 2);
    $srcWidth = $srcHeight = $originalHeight;
}

$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);
  • $newwidth=120; /// Define New widh $newheight=120; ///Define New Height I don't want to define height and width. i want auto resize – Pritesh Mahajan May 15 '14 at 07:56