0

I would like to resize an image proportionally to 500px X 500px I found the below code from the net however the article didn't have an example of how to use the script, can someone just get me started on how to display the resized image?

$filename = 'test.png';
list($width, $height, $type, $attr) = getimagesize($filename);

$orig_w = $width;
$orig_h = $height;
$MIN_W = 500;
$MIN_H = 500;
$MAX_H = 500;
$MAX_W = 500;

function image_resize_up($orig_w, $orig_h, $MIN_W, $MIN_H){
$ratio = $orig_w * 1.0 / $orig_h;   
$w_undersized = ($orig_w < $MIN_W);
$h_undersized = ($orig_h < $MIN_H);

if ($w_undersized OR $h_undersized)
{
      $new_w = round( max($MIN_W, $ratio * $MIN_H) );
      $new_h = round( max($MIN_H, $MIN_W / $ratio) );
      return array('width' => $new_w, 'height' => $new_h);
}
return null;
}

function image_resize_down($orig_w, $orig_h, $MAX_W, $MAX_H){
$ratio = $orig_w * 1.0 / $orig_h;

$w_undersized = ($orig_w > $MAX_W);
$h_undersized = ($orig_h > $MAX_H);

if ($w_undersized OR $h_undersized)
{
      $new_w = round( min($MAX_W, $ratio * $MAX_H) );
      $new_h = round( min($MAX_H, $MAX_W / $ratio) );
      return array('width' => $new_w, 'height' => $new_h);
}
return null;
}
echo $w_undersized;

I tried

echo "<img src=".$filename.$new_w.$new_h.">";//gives me undefined variable and the image isn't resized.
user3006683
  • 783
  • 2
  • 9
  • 16
  • These do the calculations for you but you need a library to actually resize the image. See imagemagick (http://php.net/imagick) or GD (http://www.php.net/manual/en/book.image.php) – user2923779 Dec 24 '13 at 02:26
  • This might help http://stackoverflow.com/questions/7393319/resize-images-with-php – Mattt Dec 24 '13 at 02:27
  • Would you like to resize the original image or just display a resized version of it on a particular webpage? And what’s `$ratio`? –  Dec 24 '13 at 02:32
  • These functions do nothing but calculate the new size for the image. You can just link to the original image and use `width`,`height` attribute or inline style like the answer. – tungd Dec 24 '13 at 02:37
  • @SharanyaDutta I just need to resize it when displaying the images. I do not need to resize and save it. – user3006683 Dec 24 '13 at 02:55
  • So, what about `echo "";`? –  Dec 24 '13 at 02:59
  • @SharanyaDutta that is not what I wanted. If I want that, I don't need a php thing, can just do it with htmL. I need to keep proportion, cropping if necessary to fit the dimension without any distortion. – user3006683 Dec 24 '13 at 03:23
  • Then you need to save the cropped version, without causing any damage to the original image. See my answer below. –  Dec 24 '13 at 07:36

4 Answers4

1

You have wrongly defined your attributes try that:

echo "<img src='".$filename."' style='width:".$new_w."; height:".$new_h."; '>";

Good Luck

Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
1

First you need a function to crop the image so that width and height are equal:

function crop_image_square($filename, $_filename){
list($width, $height, $type) = getimagesize($filename);
$min = min($width, $height);
$x = ($width > $height) ? ($width-$height)/2 : 0;
$y = ($width < $height) ? ($height-$width)/2 : 0;
    switch($type){
    case IMG_GIF:    $src = imagecreatefromgif($filename);     break;
    case IMG_JPG:    $src = imagecreatefromjpeg($filename);    break;
    case IMG_PNG:    $src = imagecreatefrompng($filename);     break;
    // and so on...
    }
$dest = imagecreatetruecolor($min, $min);
imagecopy($dest, $src, 0, 0, $x, $y, $width, $height);
    switch($type){
    case IMG_GIF:    imagegif($dest, $_filename);     break;
    case IMG_JPG:    imagejpeg($dest, $_filename);    break;
    case IMG_PNG:    imagepng($dest, $_filename);     break;
    // and so on...
    }
imagedestroy($src);
imagedestroy($dest);
}

Then you may apply the function to one or more image(s):

crop_image_square("image_01.jpg", "image_01_cropped.jpg");
crop_image_square("image_02.jpg", "image_02_cropped.jpg");
crop_image_square("image_03.jpg", "image_03_cropped.jpg");

The PHP part is done. Displaying a resized version can be easily achieved with plain HTML, as you’ve pointed out:

<img src="image_01_cropped.jpg" width="500" height="500"/>
<img src="image_02_cropped.jpg" width="500" height="500"/>
<img src="image_03_cropped.jpg" width="500" height="500"/>
1

Working with images in PHP/GD can be tedious, which is why I wrote the SimpleImage library to make things less painless. It resolves many edge cases and gotchas that come with GD image manipulation.

Here's how to resize an image proportionally to fit in a 500x500 box:

// Load the image from image.jpg
$image = new \claviska\SimpleImage('image.jpg');

// Proportionally resize to fit inside a 500x500 box and save to result.jpg
$image->bestFit(500, 500)->toFile('result.jpg');

SimpleImage can be installed via Composer or linked manually. Details in the readme.

claviska
  • 12,410
  • 2
  • 27
  • 50
  • I tried building a similar library about a year ago, but it's not even half as good as yours. Thank you. (: – tkore Jan 18 '18 at 12:50
0

If you would like, you can use the library SimpleImage.

include('src/abeautifulsite/SimpleImage.php');  //Load the SimpleImage
$image = new SimpleImage();  //Create an instance.
$image->load('picture.jpg'); //Load the picture.jpg.
image->resize(500, 500);      //Resize.
$image->save('picture2.jpg');//Save as picture2.jpg.