Simplified Image Resizing with PHP

hi there!

I’m newbie in php.
I tried to used this function but I receive the following error:

Warning: getimagesize(image): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in $file on line 30

Warning: Division by zero in $file on line 10

hi there!

I’m newbie in php.
I tried to used this function but I receive the following error:

Warning: getimagesize(image): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in $file on line 30

Warning: Division by zero in $file on line 10

call the function with $image not only with image

the function works well, however my experience has been that if you actually want the returned value (ie - html tag string) to be output to the generated page:

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

you need the “echo”.

also, users of this function should keep in mind that the image is merely resized graphically (ie: force-fit). if the original image is 300kb or 1mb, the “thumbnail” will be just as large in terms of download size.

all this function does is change the width and height in php, cmon man, a whole page dedicated to changing the width and height to be sent back ? you can do that manually or using js, a real useful function would be ACTUALLY resizing the image. so you can load say your original digital 5mb file size of all your socks ina folder, and php will take it and resize them to display and actually resize the image so the browser doesnt download 5mb size thumbnails forever

this page acting like resizing the width= and height= value is so complicated… cmon man…

For Greater Justice. Since the code in this article is not likely to be used in production here is a function that will resize an image in to a given X (width) and Y (height) value. This function requires the GD extension to be installed.

function restrainImage($path,$maxWidth,$maxHeight,$dst=null){
$imageInfo = getimagesize($path);
$width = $imageInfo[0];
$height = $imageInfo[1];
$types = array (1 => ‘gif’, ‘jpeg’, ‘png’);

$x_ratio = $maxWidth / $width;
$y_ratio = $maxHeight / $height;

if( $width &lt;= $maxWidth && $height &lt;= $maxHeight ){
    $newWidth = $width;
    $newHeight = $height;
}elseif( ($x_ratio * $height) &lt; $maxHeight){
    $newWidth = $maxWidth;
    $newHeight = ceil($x_ratio * $height);
    $newWidth = ceil($y_ratio * $width);
    $newHeight = $maxHeight;
$image = call_user_func('imagecreatefrom'.$types[$imageInfo[2]],$path);
$resized = imagecreatetruecolor($newWidth,$newHeight);


$dst is optional, if left null it will overwrite the original file. Keep in mind there is no error checking in this function and will only work if the file type is a GIF, PNG or JPG. It should give one a general idea of how to go about resizing an image file with PHP.

Any reason why this might not be working in IE? I can get it to work fine in Firefox. Would it be the functions itself being the problem or is PHP not affected meaning it could just be an html error?


This is a Server-Side script, so it should be a html-error.

I hope someone replies to this… :cool:

I want to use this function in a dynamic page ie. the images are loaded from a database using a php script so I can’t use the print function to display the image as suggested in prevoius threads. anyone know how to achieve this…

I’ve put the main part of the code below,

echo "<table class=\\"table_test\\">\
	}//end of first if
	//Display each record
		echo "<tr>\
		//get the image size of the picture and load it into an array
			$mysock = getimagesize("rivcms/news/images/image_{$row['upload_id']}06.jpg");
			//display the resized image on the screen. 
			echo "<td><img src=\\"rivcms/news/images/image_{$row['upload_id']}06.jpg\\" WHAT DO I DO HERE imageResize($mysock[0], $mysock[1], 75) />\

		echo "<td>{$row['t']}</td>\
		echo "<td>{$row['d']}</td>\
		echo "</tr>\
	$first = FALSE; //One record has been returned

thanks! :slight_smile:

You could do it two ways, from what I can see.

  1. // drop out of PHP

    <td><img src=“rivcms/news/images/image_{<?php echo($row[‘upload_id’]);?>06.jpg” <?php imageResize($mysock[0], $mysock[1], 75);?> />

    // go back into PHP

  2. echo(“<td><img src=\“rivcms/news/images/image_{$row[‘upload_id’]}06.jpg\” “);
    imageResize($mysock[0], $mysock[1], 75);
    echo(” />”);

hey guys…
i’m wondering if there is any good way to resize a bmp image with php
i’m using imagewbmp from the gd library but all i’m getting a a black resized image…

any suggestions?

please note the date that this was first published: Jul 23, 2004

Alot has changed since then!

I have a question. I have images stored in a mysql database. When I retrieve them, they’re given a URL like getimg.php?id=23243, for example, of course. I found this script does not return the right information for images that do not end in a file name. Is there any modification I can use for the function to get it working with a databased image? Please email me ASAP, csmi27 at gmail dot com, as I probably won’t check back here often. Thanks!

all this does is change the html attributes

Well I tried it and it doesn’t work. The image is the same size. However I understand how it works. Thanks

The right code is:


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 &gt; $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\\"";


//define the image source
$image_link = “”;

//get the image size of the picture and load it into an array
$mysock = getimagesize($image_link);


<!-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=“<?php echo $image_link; ?>” <?php echo imageResize($mysock[0], $mysock[1], 150); ?>>

He just forgot to put the echo function on retur, but this code still uses a simple html resize function and the picture comes out crapy anyways :frowning:

How would I get this code to work on images held in a directory.

eg - i would like all my files to be limited to 450 pixels wide when they are displayed

also where would i put this script if my current code is

[COLOR=DarkRed]//This is the query
$query = “SELECT * FROM upload2”;
$result = mysql_query ($query);

#First print the opening tag…
echo ‘<table width=“800” border=“0” cellspacing=“0” cellpadding=“0”>’;

while ($row = mysql_fetch_assoc ($result)) {
$text = $row[‘id’];
$url = $row[‘path’];
$description = $row[‘comment’];

#this will print a new row for each photo…
echo "
<td align=\“center\”><img src=\“$url\” alt=\“\”><br>$url</td>
<td align=\“left\” valign=\“middle\”>$description</td>

// This is to close database
echo “</table>”;
?> [/COLOR]

Hope you can help

I’ve used this script - thank you. I’m however having a problem with the images that are larger than the parameter passed in the function. These are photographs which are quite large. When I try to browse to the images folder to view the image I get a message stating “The image “resize.jpg” cannot be displayed, because it contains errors.”

Any ideas

PHP functions imagecopyresampled() OR imagecopyresized() can also be used for smoothly resizing images.

@QuizToon: you can use the following in your code in place of img tag:

$size = getimagesize(“$url”);
$height = $size[1];
$width = $size[0];
if ($width > 450)
$width = 450;
$percent = ($size[0] / $width);
$height = ($size[1] / $percent);
@echo “<img src=\”$url\" height=\“$height\” width=\“$width\” border=0 alt=\“\” />";
return “Picture Unavailable”;