JDT

 

FREE Downloads
Articles
Tutorials

 
Google

Using PHP to Resize an Image


The sample code given below shows how to resize a jpeg image using a PHP script.

Here is the first file, which we will call "index.php".

<?php
$picture_name = "box.jpg";
?>
<html>
<head></head>
<body>
<?php
print "<img src = display_image.php?picture_name=$picture_name />\n";
?>
</body>
</html>

This web page puts the name "box.jpg" in variable "$picture_name". The web page then inserts an image tag, using the PHP script "display_image.php" as the source for the image.

Here is the PHP script, "display_image.php":

<?php

$picture_name = $_REQUEST['picture_name'];

$max_width = 220;
$max_height = 220;

$size = GetImageSize($picture_name);
$width = $size[0];
$height = $size[1];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = ImageCreateFromJpeg($picture_name);
$dst = ImageCreatetruecolor($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
header('Content-type: image/jpeg');
ImageJpeg($dst, null, -1);
ImageDestroy($src);
ImageDestroy($dst);

?>

This script resizes "box.jpg" to 220x220 pixels.

Note: I found this script somewhere on the web, but I can't remember where. If you are the author of the script, please let the moderator of this site know, and a link will be placed to your site.







Go back to PHP Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Aimed at contractors and freelancers, Earnings Tracker enables you to perform bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 


JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions