Page 1 of 1

filename.php as an image?

Posted: Wed Oct 31, 2007 2:11 am
by drewrockshard
Hello all,

I have a php script called getimage.php. The contents is:

Code: Select all

<?
	require ('./api.class.php');
	function imageFile() {
		$client = new Client; // pay no attention to this.  This is a connection string that I took out.
		$serverList = $client->getServerList(); // same for this
		$bandwith = $client->getBandwidthimage(10702); // and this .. these are all api related things that work
		$data = $bandwith['file'];
		header("Content-type: ".$bandwith['file_type']);
		$image = base64_decode($data);
		echo $image;
	}
	imageFile();
}
?>
The $client, $serverlist, and $bandwith variables connect to a backend API and these are working. The $data variable gets info from the API, and the info that this variable gets is a base64 string for an image that is generated by an API for rrdtool. then I change the header to another API result .. which is actually image/png in this case. Then $image decodes the base64 string and I echo the $image, which displays the PNG image.

I want to resize the PNG image. I've looked into GD, however, I keep getting errors:

Code: Select all

imagecreatefrompng() [<a href='function.imagecreatefrompng'>function.imagecreatefrompng</a>]: 'getimage.php' is not a valid PNG file
 imagecopyresized(): supplied argument is not a valid Image resource 
 PHP Warning:  imagepng(): supplied argument is not a valid Image resource in
The above output is from another file that I have not listed, but that isn't the point.

My point is, and my question is, how do I go about getting the dimesions of this PNG file (filename.php)? It appears that GD sees the .php extension and doesn't think its a PNG file, however, the header is PNG file. So, I'm lost.

Any help is appriciated.

Thanks,
Drew

Posted: Wed Oct 31, 2007 3:22 am
by Christopher
Perhaps you could write you image to a temporary file with a .png extension (e.g. $filename = uniqid() . '.png') and then read it with the GD functions.

Posted: Wed Oct 31, 2007 3:59 am
by drewrockshard
Do you have an example on how to do this? Any help is appriciated.

Thanks,
Drew

Posted: Wed Oct 31, 2007 4:20 am
by Christopher

Code: Select all

$filename = '/path/to/temp/images/' . uniqid() . '.png';
file_put_contents($filename, $image);

Posted: Wed Oct 31, 2007 8:25 am
by drewrockshard
Thank you for the continued assistance. I've made some progress:

Heres's the file now:

Code: Select all

<?php
	require ('./softlayer/api.class.php');
	$filename = './tmp/' . uniqid() . '.png';
	
        function imageFile() {
		$client = new Client; // pay no attention to this.  This is a connection string that I took out.
                $serverList = $client->getServerList(); // same for this
                $bandwith = $client->getBandwidthimage(10702); // and this .. these are all api related things that work
                $data = $bandwith['file'];
		header("Content-type: ".$bandwith['file_type']);
		$image = base64_decode($data);
		global $filename;
		file_put_contents($filename, $image);
		echo $image;
	}
	
    function thumbnail($filename, $maxSize)
    {
		global $filename;
        $info = getimagesize($filename);

        $type = isset($info['type']) ? $info['type'] : $info[2];

        // Check support of file type
        if ( !(imagetypes() & $type) )
        {
            // Server does not support file type
            return false;
        }

        $width  = isset($info['width'])  ? $info['width']  : $info[0];
        $height = isset($info['height']) ? $info['height'] : $info[1];

        // Calculate aspect ratio
        $wRatio = $maxSize / $width;
        $hRatio = $maxSize / $height;

        // Using imagecreatefromstring will automatically detect the file type
        $sourceImage = imagecreatefromstring(file_get_contents($filename));

        // Calculate a proportional width and height no larger than the max size.
        if ( ($width <= $maxSize) && ($height <= $maxSize) )
        {
            // Input is smaller than thumbnail, do nothing
            return $sourceImage;
        }
        elseif ( ($wRatio * $height) < $maxSize )
        {
            // Image is horizontal
            $tHeight = ceil($wRatio * $height);
            $tWidth  = $maxSize;
        }
        else
        {
            // Image is vertical
            $tWidth  = ceil($hRatio * $width);
            $tHeight = $maxSize;
        }

        $thumb = imagecreatetruecolor($tWidth, $tHeight);

        if ( $sourceImage === false )
        {
            // Could not load image
            return false;
        }

        // Copy resampled makes a smooth thumbnail
        imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
        imagedestroy($sourceImage);

        return $thumb;
    }


    function imageToFile($im, $filename, $quality = 80)
    {
		global $filename;
        if ( !$im || file_exists($filename) )
        {
           return false;
        }

        $ext = strtolower(substr($filename, strrpos($filename, '.')));

        switch ( $ext )
        {
            case '.gif':
                imagegif($im, $filename);
                break;
            case '.jpg':
            case '.jpeg':
                imagejpeg($im, $filename, $quality);
                break;
            case '.png':
                imagepng($im, $filename);
                break;
            case '.bmp':
                imagewbmp($im, $filename);
                break;
            default:
                return false;
        }

        return true;
    }
    $im = thumbnail(imageFile(), 200);
    imageToFile($im, './tmp/temp-thumbnail.png');


I need some more help. Here's my issue. I need to resize it on the fly. I also need it to keep the aspect ratio - this is a must. This code I just posted returns no errors, and returns an image, however, it never actually resizes the image.

Thanks,
Drew

Posted: Wed Oct 31, 2007 4:11 pm
by drewrockshard
Does anyone have any additional info about how to do this?