Jump to content

Saving a resized image


RobertSubnet

Recommended Posts

Hello all. I am using the following bit of code to resize a jpg image and then display it with and img tag. The function works just fine to resize the picture. But the problem I am having is saving the image in its reduced size.

 

I have tried imagejpeg to create a new, resized image, but that did not work. My server is using GD v.2.0.28.

 

<? function setImageSize($image_file) {

   $maxSize = 155; // set this varible to max width or height

   $image_size = getimagesize($image_file,&$image_info);
   $width = $image_size[0];
   $height = $image_size[1];

   if($width > $maxSize || $height > $maxSize) {

      if($width > $maxSize) {
        $z = $width;
        $i = 0;
        while($z > $maxSize) {
          --$z; ++$i;
        }
        $imgSizeArray[0] = $z;
        $imgSizeArray[1] = $height - ($height * ($i / $width));

      } else {

        $z = $height;
        $i = 0;
        while($z > $maxSize) {
          --$z; ++$i;
        }
        $imgSizeArray[0] = $width - ($width * ($i / $height));
        $imgSizeArray[1] = $z;
      }

  } else {

     $imgSizeArray[0] = $width;
     $imgSizeArray[1] = $height;
  }

  return $imgSizeArray;
} 


$imgSize = setImageSize("archer.jpg"); 

?>

<br/>

<img src="archer.jpg" width="<? echo $imgSize[0];?>" height="<? echo $imgSize[1];?>" />

 

Any suggestions about what I am missing to save the returned file in its reduced size woudl be much appreciated.

 

Thanks!

~Robert

Link to comment
https://forums.phpfreaks.com/topic/179944-saving-a-resized-image/
Share on other sites

http://php.net/imagecopyresized

 

Look into it, it'll help you save an image resized.

 

And I'm wondering, are you simply trying to resize an image to a max height or width of $maxSize, keeping the aspect ratio?  If that's all you're doing there's a much, MUUUCHHH simpler way.

You'll still need to write your own function, yes, but the way I do it has always been simpler (in my opinion).  Keep in mind this is just the function to get the new size, not to actually save the image:

 

<?php
$maxWidth = 155; // The maximum width of the image, this should be outside the function

function imageSize($image) { // $image needs to be a valid image resource
  GLOBAL $maxWidth; // This makes it so you can use the $maxWidth variable
  $width = imagesx($image); // Gets the width of $image
  $height = imagesy($image); // Gets the height of $image
  if($origWidth > $maxWidth) {
    $diff = $maxWidth / $origWidth; // Gets the ratio of the max width to the actual width
    $width = $maxWidth; // Updates $width with the value of $maxWidth
    $height = $diff * $origHeight; // updates $height using the proper ratio
    }
  $imgSize[] = $width;
  $imgSize[] = $height;
  }

$img = imagecreatefromjpeg("archer.jpg");
$imgSize = imageSize($img);

echo "<img src=\"archer.jpg\" width=\"".$imgSize[0]."\" height=\"".$imgSize[1]."\" alt=\"\">";
?>

 

If you want to control both height and width it'll take a little modification, but not much.  If you plan on loading more than just jpgs you'll also need to do a little modification to the "imagecreatefromjpeg()" line.  What this does it read in an image, checks if the image's width is larger than the maximum acceptable width, $maxWidth.  If it is it resizes the width to the max width, then calculates the height by multiplying the actual height by the result of the $maxWidth and $origWidth, which will always be less than 1 if the actual width is bigger than the max width.

 

Ok, so anyway, let me know if you need mor help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.