Text Placement problem

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
benjyphp
Posts: 1
Joined: 2011-03-18T01:17:05-07:00
Authentication code: 8675308

Text Placement problem

Post by benjyphp »

Hello,

I am in the process of making a script for a online store which automatically generates category images. The idea is a user uploads a image, and is then presented with a page in which they can crop a selection of the image to use as a category image (http://www.codeforest.net/how-to-crop-a ... ry-and-php i modified this code) once this has happened and the user presses a preview button, they are presented with a new page with a preview of the category image.

Here comes my problem, the "category image" is basically the image the user uploaded scaled down to whatever size they chose on the jquery interface with a number of other layers added on top, one subtracts edges so it is a rounded shape, another adds a border and another adds a flash on which text needs to be displayed. This all works great. The problem comes when i want to add the text to the image. I am using the MagickAnnotateImage method and from what i understand the x and y co-ordinates you provide are from the top left hand corner of the picture. When i am adding the text it seems to treat the image as if it is the size of the original image the user uploaded.

So for example, if the user drops an image which is the very right hand bottom corner of the image they uploaded it wont show the text because i have set the text to appear 20 pixels from the left hand corner and the crop they made probably starts 100 pixels in. I have no idea why it is treating the text addition as if the picture is still the size of the original because i have split the process up into two stages so when it gets to the point of adding the text it saves the modifactions made so far onto the filesystem and then reopens it using a new magic wand. Any ideas?


below is my code, it might be easier to understand than my explantion lol
page 1

Code: Select all

$magick_wand=NewMagickWand();
        MagickReadImage($magick_wand,"../images/" . $image_info['categories_image']);
       
        MagickCropImage($magick_wand,$_POST['w'],$_POST['h'],$_POST['x1'],$_POST['y1']);
        MagickResizeImage($magick_wand,'175','125',MW_HermiteFilter,1);
        // subtract border 
          $magick_wand_subtract_border = NewMagickWand();
          MagickReadImage($magick_wand_subtract_border,'../images/categories/_image_crop_anti_border.png');
        MagickCompositeImage($magick_wand,$magick_wand_subtract_border,MW_CopyOpacityCompositeOp,0,0);
        // add border
          $magick_wand_add_border = NewMagickWand();
          MagickReadImage($magick_wand_add_border,'../images/categories/_image_crop_add_border.png');
        MagickCompositeImage($magick_wand,$magick_wand_add_border,MW_AtopCompositeOp ,0,0);
        
        // String length Test
        if(strlen($cat_name_query['categories_name'])<=100){
          // add single flash
          $magick_wand_add_single_flash = NewMagickWand();
          MagickReadImage($magick_wand_add_single_flash,'../images/categories/_image_crop_add_single_flash_border.png');
          MagickCompositeImage($magick_wand,$magick_wand_add_single_flash,MW_AtopCompositeOp,0,0);
        } else {
          // add double flash
          $magick_wand_add_double_flash = NewMagickWand();
          MagickReadImage($magick_wand_add_double_flash,'../images/categories/_image_crop_add_single_flash_border.png');
          MagickCompositeImage($magick_wand,$magick_wand_add_double_flash,MW_AtopCompositeOp,0,0);
        }        
        
        // Save image with layers on top
        MagickWriteImage( $magick_wand, '../images/temp_cat_preview/' .  $image_info['categories_image']);
page 2

Code: Select all

$magick_wand_new=NewMagickWand();
        MagickReadImage($magick_wand_new,"../images/temp_cat_preview/" . $image_info['categories_image']);

   $drawing_wand=NewDrawingWand();
          DrawSetFont($drawing_wand,"../usr/share/fonts/bitstream-vera/Vera.ttf");
          DrawSetFontSize($drawing_wand,11);
          $pixel_wand=NewPixelWand();
          PixelSetColor($pixel_wand,"white");
          DrawSetFillColor($drawing_wand,$pixel_wand);
          
        
        
        // Finally add the text 
        if (MagickAnnotateImage($magick_wand_new,$drawing_wand,20,20,0,$cat_name_query['categories_name'])!= 0)
          {
            header( 'Content-Type: image/jpg' );
            MagickEchoImageBlob( $magick_wand_new );
          }
        else
          {
            echo MagickGetExceptionString($magick_wand_new);
          }
        //unlink("../images/temp_cat_preview/" . $image_info['categories_image']);
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Text Placement problem

Post by el_supremo »

When i am adding the text it seems to treat the image as if it is the size of the original image the user uploaded.
This suggests that you need to "repage" the image after you crop or resize it so that the virtual canvas is the same size as the image.
This will do it but you'll have to figure out the best place to put it :-)

Code: Select all

MagickResetImagePage($magick_wand,"");
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Post Reply