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.
Results 1 to 4 of 4
  1. #1
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts

    Text on image and saving the new jpeg into a folder

    Below is a script that will put text onto a jpeg and output it to the browser. But I would like to save the new image with text into a folder on the server or display the new image in a html page. The original jpeg is already on the server. If I was uploading the jpeg and adding text I would be able to put it into a folder but I seem to have problems because the original image is already on the server.

    The purpose of this php script is to add the copyright information as the image is displayed. The copyright info then can be changed at anytime without having to adjust the actual jpegs, this would be good if you have 100's of images.

    PHP Code:
    <?php
    $filename 
    'test.jpg';
    header('Content-type: image/jpeg');

    $image_size getimagesize($filename);

    $image_t imagecreatetruecolor($image_size[0], $image_size[1]);
    $image imagecreatefromjpeg($filename);
    imagecopyresampled($image_t$image0000$image_size[0], $image_size[1], $image_size[0], $image_size[1]);

    $white imagecolorallocate($image_t255255255);
    $text_size '24';
    $text_x '20';
    $text_y $image_size[1]-20;
    $text 'Copywrite 2007';
    $font 'arial.ttf';
    imagettftext($image_t$text_size0$text_x$text_y$white$font$text);

    imagejpeg($image_tnull100);

    imagedestroy($image_t);
    ?>
    Leonard Whistler

  • #2
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    <?php
    $filename = 'test.jpg';
    header('Content-type: image/jpeg'); <--- remove this line
    
    $image_size = getimagesize($filename);
    
    $image_t = imagecreatetruecolor($image_size[0], $image_size[1]);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_t, $image, 0, 0, 0, 0, $image_size[0], $image_size[1], $image_size[0], $image_size[1]);
    
    $white = imagecolorallocate($image_t, 255, 255, 255);
    $text_size = '24';
    $text_x = '20';
    $text_y = $image_size[1]-20;
    $text = 'Copywrite 2007';
    $font = 'arial.ttf';
    imagettftext($image_t, $text_size, 0, $text_x, $text_y, $white, $font, $text);
    
    imagejpeg($image_t, null, 100); <--- change this line
    
    imagedestroy($image_t);
    ?>
    Those highlighted lines are the ones you need to correct. Obviously you should wrap this up into a function as well.
    Quote Originally Posted by PHP Manual
    int imagejpeg ( resource image [, string filename [, int quality]])
    imagejpeg() creates the JPEG file in filename from the image image. The image argument is the return from the imagecreate() function.

    The filename argument is optional, and if left off, the raw image stream will be output directly.

    Seeing as you won't be dumping the stream to the client you won't need to post jpeg headers.
    Omnis mico antequam dominus Spookster!

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Thanks Mhtml.....Script works perfectly now with those changes and I will look into the function as well.
    Leonard Whistler

  • #4
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    No problems mate.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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