Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts

    PHP GD library error

    Well, I have an image ans I want to write a text on it. The issue is, it is saying,
    image cannot be displayed because it contains errors. Refer to the screenshot below as attachment.

    Here the codes:

    Code:
    <img src="images/img.jpg" width="500" height="300">
    Code:
    <?php
    header ("Content-type: image/jpeg");
    $string = "This is my text";
    $font = 4;
    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefromjpeg("images/img.jpg");
    $x = imagesx($im) - $width ;
    $y = imagesy($im) - $height;
    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y, $string, $textColor);
    imagejpeg($im);
    ?>
    Attached Thumbnails Attached Thumbnails PHP GD library error-untitled.jpg  

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You'll need to check your error logs or add:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    to the top and comment out the header for the content type, then access the image directly. One or more of these are throwing an error, suspicion goes to imagecreatefromjpeg failing, and since you have no other error handling the rest would all throw errors too.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I made this:

    Code:
    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    //header ("Content-type: image/jpeg");
    $string = "This is my text";
    $font = 4;
    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefromjpeg("images/img.jpg");
    $x = imagesx($im) - $width ;
    $y = imagesy($im) - $height;
    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y, $string, $textColor);
    imagejpeg($im);
    ?>
    It gives me rubbish codes of the image.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yes that would be the text of the image binary. Errors would show up first though, does it show errors? If not, check the source and verify that there is no preceding whitespace. If there is, then you have a space before the <?php in the script.

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Here is a screenshot of the part of the codes. What you mean by white space? I see nothing which has white space, see it below:
    Attached Thumbnails Attached Thumbnails PHP GD library error-sss019.jpg  

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Whitespace = any whitespace character. If you contain output with whitespace it will corrupt the image binary.
    That image is useless as it contains no line numbers in it. I mean check the textual output of the image to see if there is preceding whitespace before the imagetype binary as well as any other errors.

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    I showed you the part of the codes. I don't see any issue here on whitespace. See the attachment

  • #8
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    Fou-Lu is not looking for part of the code on your backend. He is pointing at whitespace BEFORE you set the header. EG if your full code looks like this:

    PHP Code:
    1. 
    2. <?php
    3.   header
    ('bla bla');
    4.   code code
    5. ?>
    it will not work, as you have whitespace outputted before you set your header. As your screenshot only centers around your part of the code, we can not judge whether this is the only code on that page, or whether there is more somewhere which could be influential.
    Last edited by Thyrosis; 11-30-2012 at 02:13 PM.

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    It should be like this below or what? Sorry, but it's late here and since morning I am on this computer, so I may not understood you.

    Code:
    <?php header ("Content-type: image/jpeg");
    $string = "This is my text";
    $font = 4;
    $width = imagefontwidth($font) * strlen($string) ;
    $height = imagefontheight($font) ;
    $im = imagecreatefromjpeg("images/img.jpg");
    $x = imagesx($im) - $width ;
    $y = imagesy($im) - $height;
    $backgroundColor = imagecolorallocate ($im, 255, 255, 255);
    $textColor = imagecolorallocate ($im, 0, 0,0);
    imagestring ($im, $font, $x, $y, $string, $textColor);
    imagejpeg($im);?>

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Lets try this again.
    Quote Originally Posted by Fou-Lu View Post
    That image is useless as it contains no line numbers in it.
    Make sure you attach directly to this script.

    Edit:
    Just check your error logs. It will be a lot easier.
    Last edited by Fou-Lu; 11-30-2012 at 02:17 PM.

  • #11
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Here a new screenshot with line numbers:
    Attached Thumbnails Attached Thumbnails PHP GD library error-sss020.jpg  

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Look at all the whitespace there. Who knows what's above that.
    You cannot embed a GD script with the output type as an image into a textual output script. The script's entire purpose is to generate and represent itself as an image. That should look more like this:
    1. <?php
    2. header('Content-type: image/jpeg');
    3. ...

    Where the bullet number is your line number.

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Ok, I cut the whole script and paste it above the doctype. Still not work.

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by angelali View Post
    Ok, I cut the whole script and paste it above the doctype. Still not work.
    Read again. You CANNOT embed a script representing itself as an image in a textual output script. You MUST give it a script of its own. You can have one and only one content type on an output header.

  • #15
    Regular Coder
    Join Date
    Sep 2011
    Posts
    361
    Thanks
    39
    Thanked 0 Times in 0 Posts
    Facepalm to myself. I need a weed I think. My head is too heavy to understand thing. So, if I copy and paste the script in another page then INCLUDE the page to the one, will it work?


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •