tags:

views:

100

answers:

5

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); ?>>
+1  A: 

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.

Jason Leveille
I want to know how I can to check and see if the images width is smaller then 100px if so don't re-size.
seeu
sorry about the confusion.
seeu
If only it worked like in the movies *Zoom! Enhance! Rotate 90 degrees into the z dimension!*
alex
So, really, what you're asking is to check if the image is > 100px. If so, resize to 100px width? In that case, check the width of the image. If greater than 100px, set the width of the image to 100px using CSS. Otherwise, leave it alone. The only time this could be a problem is if you have a large filesize (and want to avoid loading a 5MB image).
Jason Leveille
Have you ever compared the quality of an image that is redrawn by PHP vs rendered smaller through HTML? Redrawing in PHP is always preferable to resizing on the client side.
Noah Goodrich
I'm well aware of the problems with image interpolation. I'm making a suggestion. In some cases it makes more sense to just use CSS for the job. This may or may not be the case here, but it's worth noting.
Jason Leveille
gabriel1836 doesn't seem to understand that there is no image resizing happening, that it's just scaling done for display purposes.
Myles
+1  A: 

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.

Jorge Israel Peña
I read and tried that code as you can see above the code is from there but I have played around with it but it won't re-size the image for some reason?
seeu
Don't reinvent the wheel. Use ImageMagick!
Alex
A: 

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.

Myles
Client-side manipulation is never a better option than server-side image manipulation.
Noah Goodrich
He's not manipulating the image, he's getting the size of it and setting his CSS according for display purposes. If you read the question you'd have gotten that.
Myles
+1  A: 

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.

philfreo
+1  A: 

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);
 } 
}
Noah Goodrich