/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Calling php file from another php file + GD library

I was trying to print on screen an image within a div by doing this in my layout php file:

[code=php]
<?php header(“Content-type: image/jpeg”); ?>

<img src=”index.php?var=<?php echo $value; ?>” />
[/code]

but something isn’t going as expected.

the invoked code does this at the end:

[code=php]
$success = imagejpeg($image, ‘images/postcards/postcard.jpg’);
$success = imagejpeg($image);
imagedestroy($image);
[/code]

As you can see, I’m trying to do two different things with the image:
1- To dump it and save it in a jpeg file, which is working right
2- To directly output the data as a jpg image on the browser. This is not working right. Despite the second line returns true as well as the first line does, the image is not displayed.

Why could the first choice be wrong and not display the image?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 14.2010 — You cannot output the image such as with imagejpeg() within a file that is outputting anything else, including HTML <img> tags. If you are going to output it via imagejpeg(), then that is [i]all[/i] that file should output (with the applicable content-type header). Therefore, your separate main script would have an <img> tag whose src attribute calls the image-generating script.

Alternatively, if you are saving the file anyway, then just create an <img> tag that points to that saved file instead of outputting it with imagejpeg().
Copy linkTweet thisAlerts:
@lmartinLMVDec 14.2010 — OOps! hey NogDog thanks for replying, but I mixed up the code in the post. Please excuse my mistake. :o

The [B]header()[/B] line is actually in the invoked file which manages the GD library functions, at the beginning.

The file which has the <img> tag is in reality a php file which outputs an HTML document.

So now, why isn't this working?
Copy linkTweet thisAlerts:
@JazztronikauthorDec 14.2010 — Note: I'm the same user as above (the one above us my user at work).
Copy linkTweet thisAlerts:
@NogDogDec 14.2010 — One thought is to make sure that your image file was not saved as UTF8 with a BOM (byte order mark), as the BOM will corrupt the image output.
Copy linkTweet thisAlerts:
@lmartinLMVDec 14.2010 — I'm approaching the problem.

It's not due to the function imagejpeg(), but to another function I'm using prior to this one (I ommitted it in previous posts because I thought the problem was not there). It's this function:

[code=php]$success = imagettftext($image, 20, 0, 0, 0, $black, $font, 'My simple text');[/code]

where $image is the image resource, $black the integer value corresponfing to the black colour, and $font is 'arial' (I also tried with 'arial.ttf').

This function returns false (demonstrating it's not working) and most likely is what is avoiding the image to be displayed. Moreover if I comment out this line of code, the rest of the image, the background, is displayed correctly.

I'm quite sure the problem occurs because of the font. The Arial font is TrueType, so this shouldn't be a problem. In addition, I've tried both by using installed fonts in the system, without changing the GDFONTPATH environment variable, and using the arial.ttf font file in the web path by doing this:

[code=php]putenv('GDFONTPATH=' . realpath('./fonts'));[/code]

where the arial.ttf is in /fonts from the web root.

Suggestions please?
Copy linkTweet thisAlerts:
@NogDogDec 14.2010 — Ugh...I've seen lots of people complaining about getting that function to work. I've not used it myself (or at least not in quite some time, I think), so all I can suggest is to do what I'd so: search through all the user comments on that function's manual page -- pending someone coming along here who has more experience with it. ?
Copy linkTweet thisAlerts:
@JazztronikauthorDec 14.2010 — No problem mate! You tried helping, and moreover you helped me many times before so I'm already grateful to you. ?

Anyway I'll check the page corresponding to this function in php.net. Maybe I'll find something that helps. ?

Cheers!
Copy linkTweet thisAlerts:
@JazztronikauthorDec 15.2010 — I finally fixed the problem! Although it becomes a drawback for PHP.

I had to change the header from
[code=php]<?php header("Content-type: image/jpeg"); ?>[/code]
to
[code=php]<?php header("Content-type: image/png"); ?>[/code]

and use the analogous functions for PNG, and use PNG's as backgrounds instead of JPG's. But this was not the only problem. If I used PNG-8 backgrounds, the characters printed on them had the same colour as the background, just a little darker. So the solution was to use PNG-24 images. Thank goodness I found it out by chance because I noticed I saved one of the backgrounds as PNG-24 by mistake (PNG-24 with large images are too heavy) and this one was the only one working.

Plus, very few fonts produced the desired result. I had to discard some good fonts for a Christmas postcard and use Arial instead.

I'm kind of glad to have found a way around, but I'm not as content as I'd like. I think the GD library should be improved quite a lot. The final backgrounds are 5-6 times heavier than a JPG with many gradients at 60&#37; quality. That's not a good practice.
×

Success!

Help @Jazztronik spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...