PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Nov 13, 2018 4:20 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Oct 31, 2007 2:11 am 
Offline
Forum Commoner

Joined: Sat May 29, 2004 6:07 pm
Posts: 37
Location: Dallas, Texas
Hello all,

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

Syntax: [ Download ] [ Hide ]
<?

        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:

Syntax: [ Download ] [ Hide ]
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 3:22 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13583
Location: New York, NY, US
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.

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 3:59 am 
Offline
Forum Commoner

Joined: Sat May 29, 2004 6:07 pm
Posts: 37
Location: Dallas, Texas
Do you have an example on how to do this? Any help is appriciated.

Thanks,
Drew


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 4:20 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13583
Location: New York, NY, US
Syntax: [ Download ] [ Hide ]
$filename = '/path/to/temp/images/' . uniqid() . '.png';

file_put_contents($filename, $image);

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 8:25 am 
Offline
Forum Commoner

Joined: Sat May 29, 2004 6:07 pm
Posts: 37
Location: Dallas, Texas
Thank you for the continued assistance. I've made some progress:

Heres's the file now:

Syntax: [ Download ] [ Hide ]
<?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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 4:11 pm 
Offline
Forum Commoner

Joined: Sat May 29, 2004 6:07 pm
Posts: 37
Location: Dallas, Texas
Does anyone have any additional info about how to do this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group