seeu
seeu

Reputation: 11

Image resizing question using php?

I was wondering how can I create a piece of PHP code that will check a jpg, gif, png or any other types of images using the getimagesize() .

And then check if the images width is smaller then the width in the following CSS code below if so then the PHP code should re-size the image all while keeping the images aspect ratio, no matter if its in landscape or portrait.

CSS code

overflow: hidden;  width:100px; height: auto;

I found this piece of code online listed below it seems to be using the getimagesize() but it does not seem to re-size the image. Is there a way I can fix this.

<?php

function imageResize($width, $height, $target) {

//takes the larger size of the width and height and applies the  
//formula accordingly...this is so this script will work  
//dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the

return "width=\"$width\" height=\"$height\"";

}

?>

<?php

//get the image size of the picture and load it into an array
$mysock = getimagesize("images/sock001.jpg");

?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php imageResize($mysock[0],  
$mysock[1], 150); ?>>

Upvotes: 1

Views: 554

Answers (5)

Noah Goodrich
Noah Goodrich

Reputation: 25263

I built a custom Image class for our in-house framework that uses code from Simon Jarvis for the basics of image resizing.

You can find the sample code and a tutorial here:

http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php

Basically the magic for what you want to do comes down to the following methods that I use in our image class (Any class properties are set elsewhere, but you can just hard-code these values if you need to):

public function resize($width,$height) 
{
  $new_image = imagecreatetruecolor($width, $height);
  imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  $this->_image = $new_image;

  unset($new_image);

  return $this;   
}

public function resizeToHeight($height) 
{
  $ratio = $height / $this->getHeight();
  $width = $this->getWidth() * $ratio;
  $this->resize($width,$height);

  return $this;
}

public function resizeToWidth($width) 
{
  $ratio = $width / $this->getWidth();
  $height = $this->getheight() * $ratio;
  $this->resize($width,$height);

  return $this;
}

public function output()
{   
    if($this->_image) {
        ob_clean();
        ob_end_clean();

        // Start sending headers
        header("Pragma: public"); // required
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false); // required for certain browsers
        header("Content-Transfer-Encoding: binary");
        header("Content-Type: " . $this->_settings['mime']);

        $function = 'image' . substr($this->_settings['mime'], 6); 
        $function($this->_image);
    }   
}

Upvotes: 1

philfreo
philfreo

Reputation: 43804

Your question is how to ask the server if an image is greater than a certain width, so that you know not to use CSS/HTML to resize-up those images.

A better approach could be to use the server to proportionally downsize any images that are too large. If you can trust that your server will always do this, than you don't need to do any resizing in the browser.

Upvotes: 1

Myles
Myles

Reputation: 21510

You might see if your server has ImageMagick or gd installed, or any number of other PHP image libraries.

Another option, which is probably better, is to do the image manipulation in javascript.

Upvotes: 0

Jorge Israel Pe&#241;a
Jorge Israel Pe&#241;a

Reputation: 38586

You can try using the PHP ImageMagick Library's resizeImage method, or perhaps better yet the scaleImage method. That is if my understanding is correct, that you literally want to resize an image.

If you are talking about merely resizing it on the presentation phase, then this article seems to talk about that. It gets the dimensions using getimagesize and then returns a string which you pass to the img tag accordingly. I'm sure this would be of some use to you.

Upvotes: 1

Jason Leveille
Jason Leveille

Reputation: 1190

You might as well just set the size of the image using CSS. Either way, when you scale the image up the quality of the image will degrade.

Upvotes: 1

Related Questions