image hosting help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rev0
Forum Newbie
Posts: 4
Joined: Sat Aug 08, 2009 10:38 pm

image hosting help

Post by Rev0 »

Ok so Ive written a script that creates an image based on info from the url. I want people to be able to type something like this into a fourm and for my script to return an image.

Code: Select all

[img]http://127.0.0.1/Main.php?u=463654&tc=225000225&bc=100225100[/\img]
Do I need to actully host the image on the server or can I just return the image?
If so how do I go about this because when I try:

Code: Select all

imagejpeg($im);
all I get is an encoded string like this:

Code: Select all

??.$8?j??? V????j??Cs&zg?}*?k????=WQv?[? ?t ??|????>???9??K?z?8bU?????R?t?H??n?X?`(???I>?gk???,K?x??u-M?W?Q?w_ufMWSeQ?"?B???K?I.??p?9??????I?x?;h?
Instead of an actual image.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: image hosting help

Post by cpetercarter »

Have you tried to save the image to a file instead of outputting it direct?
Rev0
Forum Newbie
Posts: 4
Joined: Sat Aug 08, 2009 10:38 pm

Re: image hosting help

Post by Rev0 »

Yes. Ive been able to edit the image and save it to the HDD.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: image hosting help

Post by McInfo »

Do you have a Content-Type header in your script?

Code: Select all

header('Content-Type: image/jpeg');
If that doesn't help, please post your script so we can better understand what is happening.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:07 pm, edited 1 time in total.
Rev0
Forum Newbie
Posts: 4
Joined: Sat Aug 08, 2009 10:38 pm

Re: image hosting help

Post by Rev0 »

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


K, I simplified it a little by taking some of the repetitive code out.
[EDIT] ive commented the code a little better so its easier to understand

Code: Select all

Function GenerateIMG($ml,$c,$ini_array)
    {
        $path = "C:\\Users\\Administrator\\Pictures\\sig.jpeg";
        if (!file_exists($path)) {
           echo 'File does not exist.';
        } else {
           $im = imagecreatefromjpeg($path);
    if (!headers_sent()) {
        header('Content-type: image/jpeg');
        imagejpeg($im);
    }
       }
        
        imagedestroy($im);
       

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
Last edited by Rev0 on Thu Aug 27, 2009 1:26 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: image hosting help

Post by McInfo »

Remove the call to header() from line 10. As it is now, if your script outputs error messages, they will be included in the body of the image. This will cause the browser to display garbage.

Replace line 60 with

Code: Select all

if (!headers_sent()) {
    header('Content-Type: image/jpeg');
    imagejpeg($im);
}
If everything works perfectly, you will see the image. If there are errors, you will see the errors instead of the image.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:08 pm, edited 1 time in total.
Rev0
Forum Newbie
Posts: 4
Joined: Sat Aug 08, 2009 10:38 pm

Re: image hosting help

Post by Rev0 »

Thanks for the input McInfo but its still not working :/
For debugging purposes Ive narrowed my code down to this to exclude anything that might be complicating the output, but its still spitting out jibberish.

Code: Select all

<?php
        $im = @imagecreatefromjpeg("C:\\Users\\Admin\\Pictures\\sig.jpeg");
        
        if($im)
        {
            If (!headers_sent()) {
                    header('Content-type: image/jpeg');
                    imageJPEG($im);
                    imageJPEG($im,"Saved.jpeg");
                }       
        }
?>

Could someone run this script on their comp for me and see if it works? Im thinking its my computer because this shouldnt be so complicated.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: image hosting help

Post by McInfo »

The script worked for me.

The error suppression operator (@) does not help when debugging.

Try this.

Code: Select all

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
$path = "C:\\Users\\Admin\\Pictures\\sig.jpeg";
if (!file_exists($path)) {
    echo 'File does not exist.';
} else {
    $im = imagecreatefromjpeg($path);
    if (is_resource($im) && !headers_sent()) {
        header('Content-type: image/jpeg');
        imagejpeg($im);
    }
}
?>
Edit: This post was recovered from search engine cache.
Post Reply