Jump to content

[SOLVED] text onto jpg error


graham23s

Recommended Posts

Hi Guys,

 

i did some pretty basic testing with GD , this code should overlay a watermark/text on uploaded images but im getting a few errors:

 

part of the code:

 

<?php
$uploaddirectory = "uploads/".$renamedimage;

	  // Get identifier for white
          $white = imagecolorallocate($uploaddirectory, 255, 255, 255);

          // Add text to image
          imagestring($uploaddirectory, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white);
?>

 

but i'm getting these errors:

 

Warning: imagecolorallocate(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 78

Warning: imagesy(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 81

Warning: imagestring(): supplied argument is not a valid Image resource in /home/.castle/graham23s/www.site.com/uploadpic.php on line 81

 

not sure what to make of the errors exactly, does it mean the image isn't getting picked up by the gd functions?

 

cheers guys

 

Graham

Link to comment
Share on other sites

Well, the imagecolorallocate function doesn't want a string thtas the path to the image... but it wants a resource handler. 

On php.net under imagecolorallocate it says...

int imagecolorallocate  ( resource $image  , int $red  , int $green  , int $blue  )

 

An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

 

So fixing that... will help a lot.

Link to comment
Share on other sites

<?php
$uploaddirectory = "uploads/".$renamedimage;

	  // Get identifier for white
          $white = imagecolorallocate($uploaddirectory, 255, 255, 255);

          // Add text to image
          imagestring($uploaddirectory, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white);
?>

 

At the moment you don't have an image..

 

you need:

 

<?php 

//$image = ImageCreate(800,600); 
$image = CreateImageFromJpeg("uploads".$renamedimage); // file extension must be .jpg

$white = imagecolorallocate($image, 255, 255, 255);

imagestring($image, 3, 5, imagesy($uploaddirectory)-20, 'www.myliveaudition.com', $white);

?>

 

In theory, that should work.

 

Link to comment
Share on other sites

i see so i need to actually load the image be it whatever extension, CreateImageFromJpeg essentially recreates the exact same image to work with is that right?

 

also can the text be written to the already saved image? or would i need to re-save it again?

 

thanks a lot guys

 

Graham

Link to comment
Share on other sites

i see so i need to actually load the image be it whatever extension, CreateImageFromJpeg essentially recreates the exact same image to work with is that right?

 

 

Yes, but no not as such..

 

jpg images must be loaded through CreateImageFromJpeg();

png images through CreateImageFromPng();

gif images through CreateImageFromGif();

 

As far as I'm aware if you load a jpg image through CreateImageFromGif(), it won't work. (Or that's how it is in theory anyway)

 

also can the text be written to the already saved image? or would i need to re-save it again?

 

It won't automatically save it again. That's a part I missed off before.

 

You need to add to the bottom of your code.

 

<?php 

header("Content-Type: image/jpeg"); // tell the browser what we're going to give it - jpeg image, in this case.
ImageJpeg($image); // paint the image in browser - this doesn't save the file but shows it in the browser
ImageJpeg($image, "./newimage.jpg"); //export as jpg file - saves it as the filename you give it
ImageDestroy($image); // clean up resources
?>

 

Hope that helps?

Link to comment
Share on other sites

it does indeed mate i am definately getting there i tried to make this a function:

 

calling the function:

 

<?php
    ## watermark function
    watermark_image($filename,$uploaddirectory);
?>

 

This passes the $filename of the uploaded file to get the extension

 

function:

 

<?php
     function watermark_image($filename,$uploaddirectory) { 
     
        ## Using GD
        ## Find out the files extension 
        $extension = explode(".", $filename); 
        $extension = $extension[count($extension)-1]; 
        
        ## Make all filenames lowercase
        $extension = strtolower($extension);    
        
          ## Generate image accordingly  
          if($extension == "jpg" || $extension == "jpeg" || $extension == "pjpeg") {
          
          $image = imagecreatefromjpeg($uploaddirectory); 
          
            } elseif($extension == "png") {        
          
          $image = imagecreatefrompng($uploaddirectory); 
          
            } elseif($extension == "gif") {
            
          $image = imagecreatefromgif($uploaddirectory); 
            
            }  
            
          $white = imagecolorallocate($image, 255, 255, 255);

          imagestring($image, 3, 5, imagesy($image)-20, 'www.myliveaudition.com', $white);
          
          echo $extension;
        
        
        }
?>

 

this all looks ok , but when i test it, it just uploads without any errors (but doesn't overlay the etxt) so theres no errors to speak of, if i echo out the extension it's fine

 

can you see any errors at all?

 

thanks mate

 

Graham

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.