communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "image handling" - download "resize offsite image"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

image handling - resize offsite image was created/last modified on Sat 08 May 2004 6:11 PM

<?php
    
    
// make sure there is NO output before this script, no blankspace, no nothing
    // or you'll get a "headers already sent" error
    
    // the image from another site
    
$off_site = 'http://fundisom.com/supersonnig/nyc/4.jpg';
    
    
// read binary stream
    
$fp = fopen ($off_site, 'rb') or die('Unable to open file '.$off_site.' for reading');
    while (!
feof ($fp))
    {
        
$buf .= fgets($fp, 4096);
    }
    
    
// output header
    
header('Content-Type: image/jpeg');
    
    
$data = $buf;
    
    
//set new height
    
$size = 150;
    
$src = imagecreatefromstring ($data);
    
$width = imagesx($src);
    
$height = imagesy($src);
    
$aspect_ratio = $height/$width;
    
    
//start resizing
    
if ($height <= $size) {
        
$new_w = $width;
        
$new_h = $height;
    } else {
        
$new_h = $size;
        
$new_w = abs($new_h / $aspect_ratio);
    }
    
    
$img = imagecreatetruecolor ($new_w,$new_h);
    
    
//output image
    
imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);
    
    
// determine image type and send it to the browser
    
imagejpeg($img, '', 90);
    
imagedestroy($img);
    
?>

learn more about some of the PHP functions used in this snippet: abs, copy, die, else, end, feof, fgets, file, fopen, for, header, imagecopy, imagecopyresampled, imagecreate, imagecreatefromstring, imagecreatetruecolor, imagedestroy, imagejpeg, imagesx, imagesy, min, send, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

go to PHParadise to get free PHP, Javascript and FlashMX code