Dynadot โ€” .com Registration $8.99

Image Resizing Script

Spaceship Spaceship
Watch
Impact
49
PHP:
<?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://news.google.com/images/news.gif'; 
     
    // 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 = 20; 
    $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); 
     
?>

Would like to seek for advice.
The script is working for
$off_site = 'http://news.google.com/images/news.gif';

But if I set the $off_site = 'http://211.25.186.7/snap.jpg'
It doesn't work.

Any idea?

Thanks in advance!!!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
If you want to output a GIF image you should use imagegif at the end, imagejpeg for jpg, imagepng for png etc
 
0
•••
As for "http://211.25.186.7/snap.jpg", it appears to be a problem with that particular IP - I've tried a few other IP's and it works fine.

Here's my little edit to the script, however:

PHP:
<?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://news.google.com/images/news.gif'; 

// File extension - determined automagically
$file_extension = substr(strrchr($off_site, '.'), 1);

// ...
$data = '';

if (!($fp = @fopen($off_site, 'rb')))
{
	$file_info = parse_url($off_site);
	$file_info['path'] = (empty($file_info['path'])) ? '/' : $file_info['path'];
	$file_info['port'] = (empty($file_info['port'])) ? 80 : $file_info['port'];
	$file_info['query'] = (empty($file_info['query'])) ? '' : "?$file_info[query]";

	if (!($fp = @fsockopen($file_info['host'], 80, $errno, $errstr, 30)))
	{
		die("Could not open <code>$off_site</code> for reading!");
	}
	fputs($fp, "GET $file_info[path]$file_info[query] HTTP/1.1\r\n");
	fputs($fp, "Host: $file_info[host]\r\n");
	fputs($fp, "Connection: Close\r\n\r\n");

	while (!feof($fp))
	{
		$data .= fgets($fp, 128);
    }
    fclose($fp);

}
else
{
	while (!feof($fp))
	{
		$data .= fgets($fp, 128);
	}
}

// ...
if (!empty($data))
{
	// Output header
	switch ($file_extension)
	{
		case 'jpg':
		case 'jpeg':
			$imgheader = 'image/jpeg';
			$imgfunction = 'imagejpeg';
			break;
		case 'gif':
			$imgheader = 'image/gif';
			$imgfunction = 'imagegif';
			break;
		case 'png':
			$imgheader = 'image/png';
			$imgfunction = 'imagepng';
			break;
	}

	header("Content-Type: $imgheader"); 

	// Set new height 
	$size = 20; 
	$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
	$imgfunction($img);
	imagedestroy($img);
}

?>
 
0
•••
Nice work
Thanx alot
 
0
•••
SecondVersion,

the url itself http://211.25.186.7/snap.jpg is working...but why the script doesnt work for this ip?
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back