Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-30-2012, 01:28 PM   PM User | #1
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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
Click image for larger version

Name:	untitled.JPG
Views:	26
Size:	6.1 KB
ID:	11745  
angelali is offline   Reply With Quote
Old 11-30-2012, 01:46 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 01:51 PM   PM User | #3
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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.
angelali is offline   Reply With Quote
Old 11-30-2012, 01:52 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 01:58 PM   PM User | #5
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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
Click image for larger version

Name:	sss019.jpg
Views:	10
Size:	20.0 KB
ID:	11746  
angelali is offline   Reply With Quote
Old 11-30-2012, 02:01 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 02:07 PM   PM User | #7
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
I showed you the part of the codes. I don't see any issue here on whitespace. See the attachment
angelali is offline   Reply With Quote
Old 11-30-2012, 02:10 PM   PM User | #8
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
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..
Thyrosis is offline   Reply With Quote
Old 11-30-2012, 02:13 PM   PM User | #9
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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);?>
angelali is offline   Reply With Quote
Old 11-30-2012, 02:13 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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..
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 02:17 PM   PM User | #11
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Here a new screenshot with line numbers:
Attached Thumbnails
Click image for larger version

Name:	sss020.jpg
Views:	11
Size:	22.5 KB
ID:	11747  
angelali is offline   Reply With Quote
Old 11-30-2012, 02:19 PM   PM User | #12
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 02:21 PM   PM User | #13
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
Ok, I cut the whole script and paste it above the doctype. Still not work.
angelali is offline   Reply With Quote
Old 11-30-2012, 02:23 PM   PM User | #14
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-30-2012, 02:25 PM   PM User | #15
angelali
Regular Coder

 
Join Date: Sep 2011
Posts: 310
Thanks: 23
Thanked 0 Times in 0 Posts
angelali is an unknown quantity at this point
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?
angelali is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:38 PM.


Advertisement
Log in to turn off these ads.