Simplified Image Resizing with PHP

This is an article discussion thread for discussing the SitePoint article, “Simplified Image Resizing with PHP

didnt work for me that way
i had to put $wh = imageResize(…)
and then print_r($wh);// in the same img tag
then it worked !!!

This is cheating, any idiot can modify the IMG tag width and height attributes. What I was looking for was a PHP script that would actually generate a new image at the requested resolution. Though to be fair you do say this at the bottom. Unfortunately this article came up as the first hit on my google search :frowning:

This article was very helpfull for me!

I am not an expert in PHP so this was really the article that i was looking for.

Thank you to Greg Knox

Maybe I’m also going to use the ‘getimagesize’ function - but what I would like is that it only shows the 10 newest images from a folder - is this possible? Or that it shows randomly 10 pictures out of a folder… Appreciate any hints.

A big problem here is that you are loading the entre image, and incurring the bandwidth for it as well, when you could just be loading a thumbnail image that is a fraction of the size. Why load an 80kb image when you only need to load a 5kb image?

This is for craig34
YOu are correct however if the image is a hyperlinked thumbnail of the original and you have over 3000 images it is great. Having to resize all images (which takes up server space) and have it load two images…
This is beter.

Thank you Greg Knox

The function can be rewritten for nonpercent values and different value for width and height

What you can do instead of using up bandwidth is have php resize the image before downloading it eg:
<?php
// File and new size
$filename = ‘test.jpg’;
$percent = 0.5;

// Content type
header(‘Content-type: image/jpeg’);

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

Here is a function which is simpler to call and doesnt mix the PHP and IMG tags.

<?php
function imageResize($image, $target, $alt=NULL) {

//Gets the width and height of the image and outputs it as $theimage[0] (width) and $theimage[1] (height)
$theimage = getimagesize($image);
$width = $theimage[0];
$height = $theimage[1];

//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 inside an image tag so you can call it with “imageResize(“images/sock001.jpg”, “Some alt text”, $target)”

echo “<img src=\”$image\" width=\“$width\” height=\“$height\” alt=\“$alt\”>";
}
?>

<!- Now, you can simply use your imageResize function instead of mixing up your PHP with an IMG tag –>

<?php imageResize(“images/sock001.jpg”, 50, “Oh what a beautiful sock”); ?>

In the above article Posted by: DatraxMada , following

$width = $theimage;
$height = $theimage;

should ble like this ;

$width = $theimage[0];
$height = $theimage[1];

hi all,

in DatraxMada code, the following



$width = $theimage;
$height = $theimage;


the $theimage is an array, therefore

the code should be changed to this



$width = $theimage[0];
$height = $theimage[1];


Chris Davidsen

Are you people sure that the code is:

$width = $theimage;
$height = $theimage;

and not:

$width = $theimage;
$height = $theimage;

???

I’m seriously lost… help!!!

I tried the above article out, but found that it was not working that well for me. I’ve included an updated script that works really well for me. By the way, forgive me by the messiness of the code - I was in a rush! :slight_smile:


// Max width/height of image.
$maxImgXY = "125"; 

// Grab image details
$arrFileSize = getimagesize("filename");

// Check if both sides of image exceed allowable proportions.
if($arrFileSize[0] < $maxImgXY && $arrFileSize[1] < $maxImgXY) {
    $imgWidth  = $arrFileSize[0];
    $imgHeight = $arrFileSize[1];
}
else {
// Find the largest side (width/height).
if($arrFileSize[0] > $arrFileSize[1]) {
    $imgLarge = $arrFileSize[0];
    $imgWidth = $maxImgXY;

// Divide that side by the maximum size allowed.
    $aspectRatio = $imgLarge / $maxImgXY;

// Determine size of remaining side (height) using the result above.
    $imgHeight = round($arrFileSize[1] / $aspectRatio);
}
else {
    $imgLarge  = $arrFileSize[1];
    $imgHeight = $maxImgXY;

// Divide that side by the maximum size allowed.
    $aspectRatio = $imgLarge / $maxImgXY;

// Determine size of remaining side (width) using the result above.
    $imgWidth = round($arrFileSize[0] / $aspectRatio);
}
}

The problem in the original code is this line:

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

You need to print the results of the function call rather than just call the function. So it would end up looking like this:

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

Can you explain how you would use this if you were grabbing the images from the images folder and then printing them into a results table?

show us how to resize the image and make it a smaller file size, ya bum! :slight_smile:

Thanks so much for this! I was looking for ages for a soltion to my problem (how to resize images on the fly to use as thumbnails).

Great

just what i was looking for :slight_smile:

Haha. To resize the image and make it a smaller filesize, use a free program like “Irfanview”. You can choose to resample the image for better quality, and compress the image at varying percentages to achieve truly miniscule filesizes!

This article was amazing, thanks! =D

This isn’t code I want to find, it work but it’s size is same when i get thumbnail