SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Write product name on image.

    Hi,

    I want to write name of product on image using php script.

    I did search on Google and try some script but no result.

    I have attached blank image and want to write name on this image.

    Any idea?

    -Thanks
    Attached Images Attached Images

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You can write on an image using GD or Imagemagick. Imagemagick has more options and I think simpler to use but it is not installed with php and some people are paranoid about using it as it's an external program.

    What code did you use?

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rubble,

    I try many example code from Google but no result.

    If could find good working example for me ,I will appreciate it.

    -Thanks

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    What errors did you get and what did not work?

    GD example:
    PHP Code:
    <?php  
    $canvas 
    imagecreate200100 );  

    $black imagecolorallocate$canvas00);  
    $white imagecolorallocate$canvas255255255 );  

    imagefilledrectangle$canvas00200100$white );  

    $font "../fonts/verdana.ttf";  
    $text "Sunflower";  
    $size "30";  
    $degrees "30";  
    $photo"../original_images/sunflower.jpg"

    $box imagettfbbox$size0$font$text );  
    $x = (200 - ($box[2] - $box[0])) / 2;  
    $y = (100 - ($box[1] - $box[7])) / 2;  
    $y -= $box[7];  

    imageTTFText$canvas$size0$x$y$black$font$text );  

    imagecolortransparent $canvas$white );  

    imagerotate$canvas$degrees0);  

    imagepng$canvas"temp.png" );  

    ImageDestroy$canvas );  

    $watermark imagecreatefrompng('temp.png');    
    $watermark_width imagesx($watermark);    
    $watermark_height imagesy($watermark);    
    $image imagecreatetruecolor($watermark_width$watermark_height);    
    $image imagecreatefromjpeg($photo);    
    $size getimagesize($photo);    
    $dest_x $size[0] - $watermark_width 100;    
    $dest_y $size[1] - $watermark_height 200;    
    imagecopymerge($image$watermark$dest_x$dest_y00$watermark_width$watermark_height50);    
    imagejpeg($image'watermark_GD.jpg');    
    imagedestroy($image);    
    imagedestroy($watermark);    

    unlink'temp.png');  
    ?>
    Imagemagick example:
    PHP Code:
    <?php 

    $font 
    "../fonts/verdana.ttf";  
    $text "Sunflower";  
    $size "40";  
    $degrees "30";  
    $photo"../original_images/sunflower.jpg"

    exec("convert $photo -font $font -pointsize $size -fill rgba(0,0,0,0.4) // 
    -gravity north -annotate +0+25 
    $text watermark_IM.jpg");  
    ?>

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi Rubble,

    I try the example code and check website http://www.rubblewebs.co.uk/imagemag...s/example2.php


    Code:
    <?php
    // Create the canvas
    $canvas = imagecreate( 150, 80 );
    // First colour - this will be the default colour for the canvas
    $light_blue = imagecolorallocate( $canvas, 176, 226, 255 );
    // The second colour - to be used for the text
    $black = imagecolorallocate( $canvas, 0, 0, 0 );
    // Path to the font you are going to use
    $font = "verdana.ttf";
    // Text to write
    $text = "Text";
    // Font size
    $size = "40";
    // Add the text to the canvas
    imageTTFText( $canvas, $size, 0, 15, 60, $black, $font, $text );
    // Save as Text.jpg
    imagejpeg( $canvas, "Text.jpg" );
    // Clear the memory of the tempory image 
    ImageDestroy( $canvas );
    ?>


    Code:
    <?php  
    $canvas = imagecreate( 200, 100 );  
    
    $black = imagecolorallocate( $canvas, 0, 0, 0 );  
    $white = imagecolorallocate( $canvas, 255, 255, 255 );  
    
    imagefilledrectangle( $canvas, 0, 0, 200, 100, $white );  
    
    $font = "verdana.ttf";  
    $text = "Sunflower";  
    $size = "30";  
    $degrees = "30";  
    $photo= "productimage.jpg"; 
    
    $box = imagettfbbox( $size, 0, $font, $text );  
    $x = (200 - ($box[2] - $box[0])) / 2;  
    $y = (100 - ($box[1] - $box[7])) / 2;  
    $y -= $box[7];  
    
    imageTTFText( $canvas, $size, 0, $x, $y, $black, $font, $text );  
    
    imagecolortransparent ( $canvas, $white );  
    
    imagerotate( $canvas, $degrees, 0);  
    
    imagepng( $canvas, "temp.png" );  
    
    ImageDestroy( $canvas );  
    
    $watermark = imagecreatefrompng('temp.png');    
    $watermark_width = imagesx($watermark);    
    $watermark_height = imagesy($watermark);    
    $image = imagecreatetruecolor($watermark_width, $watermark_height);    
    $image = imagecreatefromjpeg($photo);    
    $size = getimagesize($photo);    
    $dest_x = $size[0] - $watermark_width - 100;    
    $dest_y = $size[1] - $watermark_height - 200;    
    imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);    
    imagejpeg($image, 'watermark_GD.jpg');    
    imagedestroy($image);    
    imagedestroy($watermark);    
    
    unlink( 'temp.png');  
    ?>
    When I run from my WAMP PHP Version 5.4.3 ,it shows watermark_GD.jpg image is created on my folder but text is not written

    Could you please attached all files in one folder.

    I have attached verdana.ttf,productimage.jpg already attached to run exmaple.


    I think the co-ordinates on image are missing and I writing on wrong position therefore I can't see the text.

    Please advise.

    -Thanks
    Attached Files Attached Files

  6. #6
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi,

    Following example working but when text is big then going out of image.


    Code:
    <?php
      //Set the Content Type
      header('Content-type: image/jpeg');
    
      // Create Image From Existing File
      $jpg_image = imagecreatefromjpeg('sunset.jpg');
    
      // Allocate A Color For The Text
      $white = imagecolorallocate($jpg_image, 255, 255, 255);
    
      // Set Path to Font File
      $font_path = 'verdana.TTF';
    
      // Set Text to Be Printed On Image
      $text = "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";
    
      // Print Text On Image
      imagettftext($jpg_image, 25, 0, 30, 55, $white, $font_path, $text);
    
      // Send Image to Browser
      imagejpeg($jpg_image);
    
      // Clear Memory
      imagedestroy($jpg_image);
    ?>
    I got this example from following URL

    http://www.phpforkids.com/php/php-gd...xt-writing.php

    Any idea?


    -Thanks

    sunset.jpg

  7. #7
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    You will need to modify the values in imagettftext as the current values are hard coded in.

    You will need to adjust the font size to make sure it fits and the position is a bit hit and miss unless you calculate the size of the text "bounding box" and do some other calculations to get the correct position on the image.

    It all gets very complicated in GD but hopefuly you will only need to write the code once.

  8. #8
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi,

    I have change font size ,color etc.

    PHP Code:
    <?php
      
    //Set the Content Type
      
    header('Content-type: image/jpeg');

      
    // Create Image From Existing File
      
    $jpg_image imagecreatefromjpeg('product.jpg');

      
    // Allocate A Color For The Text
      
    $white imagecolorallocate($jpg_image288288288);

      
    // Set Path to Font File
      
    $font_path 'verdana.TTF';

      
    // Set Text to Be Printed On Image
      
    $text "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";

      
    // Print Text On Image
      
    imagettftext($jpg_image1003055$white$font_path$text);

      
    // Send Image to Browser
      
    imagejpeg($jpg_image);

      
    // Clear Memory
      
    imagedestroy($jpg_image);
    ?>
    The problem is when length of text is big then it is going out of the box.

    How can I make it fit on image.

    I have attached expected image updated using photoshop.

    Any idea?
    Attached Images Attached Images

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Word wrapping might work but I would try these first:

    Code:
    // Set Text to Be Printed On Image
      $text = "Experience is a hard teacher
    because she gives the test
    first, the lesson afterwards.
    - Vernon Law";
    
    or
    
    // Set Text to Be Printed On Image
      $text = "Experience is a hard teache\nbecause she gives the\ntest first, the lesson afterwards.\n- Vernon Law";

  10. #10
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rubble,

    Many thanks for update.It works fine now.

    The next problem is how to split words so that they will fit the image on new lines.

    I have attached example images.

    PHP Code:
    <?php
      
    //Set the Content Type
      
    header('Content-type: image/jpeg');

      
    // Create Image From Existing File
      
    $jpg_image imagecreatefromjpeg('product2.jpg');

      
    // Allocate A Color For The Text
      
    $white imagecolorallocate($jpg_image288288288);

      
    // Set Path to Font File
      
    $font_path 'Roboto-Regular.ttf';
      
    // Confirmation of Lost Stolen or Destroyed Stock Certificate

    $text  ="\nConfirmation of ";
    $text .="\nLost Stolen or ";
    $text .="\nDestroyed Stock ";
    $text .="\nCertificate";

    /*
    $text  ="\nBoard Resolution";
    $text .="\nAppointing an";
    $text .="\nAuditor";
    */

    //$text = "Board Resolution \n Appointing an \n Auditor";

      // Set Text to Be Printed On Image
    //  $text = "Experience is a hard teacher because she gives the test first, the lesson afterwards. - Vernon Law";

      // Print Text On Image
      
    imagettftext($jpg_image1102035$white$font_path$text);

      
    // Send Image to Browser
      
    imagejpeg($jpg_image);

      
    // Clear Memory
      
    imagedestroy($jpg_image);
    ?>
    Any idea?

    -Thanks
    Attached Images Attached Images

  11. #11
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    This works for me:

    PHP Code:
    $text  wordwrap('Confirmation of Lost Stolen or Destroyed Stock Certificate'15"\n"); 

  12. #12
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Many thanks Rubble,

    It works

  13. #13
    SitePoint Zealot
    Join Date
    Apr 2013
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Rubble,

    Any idea how to save new image in folder .

    -Thanks

  14. #14
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,413
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    All the GD functions along with examples can be found on the php website.

    PHP Code:
    // Save Image
      
    imagejpeg($jpg_image'output.jpg'); 


Tags for this Thread

Bookmarks

Posting Permissions

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