Nepali Thito
Nepali Thito

Reputation: 11

Broken image with PHP and GD

This is what I have been trying to do but it always shows broken image. I have even enabled the error reportings but everything seems to return fine..just the image is not shown. :(

http://www.tradenepal.com.np/test.php

<?php
    //Report all Errors
    ini_set("display_errors", "1");
    error_reporting(E_ALL); 

    //Set content type
    header('content-type: image/jpeg');

    //Store the values of our date in separate variables 
    list($month, $day, $year) = explode('/', date('F/jS/Y'));

    //Load our base image 
    $image = imagecreatefrompng('calendar_blank.png');
    $image_width = imagesx($image);

    //Setup colors and font file 
    $white = imagecolorallocate($image, 255, 255, 255);
    $black = imagecolorallocate($image, 0, 0, 0);
    $font_path = 'advent_light';

    //Get the positions of the text string
    $pos_month = imagettfbbox(13, 0, $font_path, $month);
    $pos_day = imagettfbbox(25, 0, $font_path, $day);
    $pos_year = imagettfbbox(8, 0, $font_path, $year);

    //Create Month
    imagettftext($image, 13, 0, ($image_width - $pos_month[2]) / 2, 40, $white, $font_path, $month);

    //Create Day 
    imagettftext($image, 25, 0, ($image_width - $pos_day[2]) / 2, 80, $black, $font_path, $day);

    //Create Year
    imagettftext($image, 8, 0, ($image_width - $pos_year[2]) / 2, 100, $black, $font_path, $year);

    //Create final image 
    imagejpeg($image, '', 100);

    //Clear up memory;
    imagedestroy($image);
?>

Upvotes: 1

Views: 1201

Answers (2)

Thrasi
Thrasi

Reputation: 498

I had a similar problem. In my case it was the fact that I had some white spaces above my <?php tag. When I removed them it worked perfectly. I hope your problem is as simple as mine.

Upvotes: 1

Brad
Brad

Reputation: 163262

Output from your script is below. You can see this yourself if you remove your content-type header, or use a tool such as Fiddler. Also, with those "free web hosting" lines in there, you will never get this to work.



PHP Error Message
Warning: imagecreatefrompng(calendar_blank.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory in /home/a5838755/public_html/test.php on line 13

Free Web Hosting
PHP Error Message
Warning: imagesx(): supplied argument is not a valid Image resource in /home/a5838755/public_html/test.php on line 14

Free Web Hosting
PHP Error Message
Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/a5838755/public_html/test.php on line 17

Free Web Hosting
PHP Error Message
Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open font in /home/a5838755/public_html/test.php on line 22

Free Web Hosting
PHP Error Message
Warning: imagettftext() expects parameter 1 to be resource, boolean given in /home/a5838755/public_html/test.php on line 27

Free Web Hosting
PHP Error Message
Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/a5838755/public_html/test.php on line 36

Free Web Hosting
PHP Error Message
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/a5838755/public_html/test.php on line 39

Free Web Hosting

Upvotes: 1

Related Questions