Zend - The PHP Company




Resizing

Add Code


Online resized images  

Type: code fragment
Added by: Freerk
Entered: 26/09/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 13723
When you need the same image on your site several times,but in different sizes here's your solution. If you want your users to easy upload images from their digicam and store them in a default size. This is how you do it. Store images once and show them in any dimension you'd like Based on jpgtn.


Two files are needed: sampleform.php3 and image.php3
You need to have jpgtn installed. In these samplescripts jpgtn is assumed to be in the same directory as the scripts.

jpgtn can be found at: http://sunsite.anu.edu.au/archives/linux/metalab/apps/graphics/misc/

To use jpgtn in these scripts, the configurationfile of jpgtn must be modified before compiling:
In jpgtn.h change the parameter MAXSIZE (line 7) like this:
#define MAXSIZE 640 /* But can be as big as you'd like */
Then do:
make
copy jpgtn to your sampledirectory

file: image.php3
<?
header 
("Content-type: image/jpeg"); 
if (isset (
$img) and file_exists($img))
  
passthru ("./jpgtn -s $size -q $qual -S $img");
?>

file: sampleform.php3
<?
$svrimg
="sample.jpg"/* Change this to a filename that suits your needs */
$defaultsize=640;      /* Store images as 640 pixels max (width or height) */
$defaultqual=80;       /* Store images with quality 80 */

if ($save && $pcimg && file_exists($pcimg)){
    if(
file_exists($svrimg))
      
unlink($svrimg);
    
exec("./jpgtn -s $defaultsize -q $defaultqual -S $pcimg > ".$svrimg);
    
unlink ($pcimg);
}
?>
<html>
<head>
<title>Sample script for resizing images</title>
</head>
<body>
<?
if (file_exists($svrimg)){?>
<img src="image.php3?img=<?echo $svrimg;?>&qual=50&size=300" border=0>
<?}else echo "No image uploaded yet";?>

<form name="uploadform" enctype="multipart/form-data" method="post">
  <table>
    <tr>
      <td>
        Select an image file (JPEG)
      </td>
      <td>
        <input type="file" name="pcimg">
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>
        <input type="submit" name="save" value="Upload image">
      </td>
    </tr>
  </table>
</form>
</body>
</html>


Usage Example




Rate This Script





Search



This Category All Categories