Overwrite Picture in Imagick Example

Hi E's, I try to use the code in snippet code for put watermark in my pictures. I chose that code because the imagick is simple to understand.

The output of imagick is inside the variable $im, the problem is I don't want to see the picture, what I want is overwrite the picture image.jpg.
The script in line 3 calls the image 'image.jpg', what the script do is put a watermark in the image, and in the end show the image with watermark, but unfortunately don't overwrite the image 'image.jpg'.
What changes I have to do for overwrite the image (with watermark)?

Regards, JC
<?php
/* Create Imagick object */
$im = new Imagick( 'image.jpg' );
/* Create a drawing object and set the font size */
$draw = new ImagickDraw();
$draw->setFontSize( 30 );    
/* Make the watermark semi-transparent */
$draw->setFillAlpha( 0.4 );
/* Set gravity to the center. More about gravity: 
http://www.imagemagick.org/Usage/annotating/#gravity */
$draw->setGravity( Imagick::GRAVITY_CENTER );
/* Write the text on the image
Position x0,y0 (Because gravity is set to center)
Rotation 45 degrees.
*/
$im->annotateImage( $draw, 0, 0, 45, "Copyright" );
/* Output */
header( "Content-Type: image/{$im->getImageFormat()}" );
echo $im;
?>

Open in new window

LVL 4
Pedro ChagasWebmasterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blue-devCommented:
You need to use ImageMagic's writeImage() method to do that.
The following snippet should work:
<?php
/* Create Imagick object */
$im = new Imagick( 'image.jpg' );
/* Create a drawing object and set the font size */
$draw = new ImagickDraw();
$draw->setFontSize( 30 );    
/* Make the watermark semi-transparent */
$draw->setFillAlpha( 0.4 );
/* Set gravity to the center. More about gravity: 
http://www.imagemagick.org/Usage/annotating/#gravity */
$draw->setGravity( Imagick::GRAVITY_CENTER );
/* Write the text on the image
Position x0,y0 (Because gravity is set to center)
Rotation 45 degrees.
*/
$im->annotateImage( $draw, 0, 0, 45, "Copyright" );
/* Output: delete because you only want so save it */

//save the image
$im->writeImage('image.jpg'); //will save the image to "image.jpg"

/*
 * If you want both, the edited and the unedited image, just save
 * it to another file name.
 *
 * Example: $im->writeImage('other_image.jpg');
 */
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pedro ChagasWebmasterAuthor Commented:
Hi, if I use the same name like image.jpg I get this error:
============================================
Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `/home/luis/public_html/pre/teste/imagemagick/image.jpg': Permission denied' in /home/luis/public_html/pre/teste/imagemagick/index.php:20 Stack trace: #0 /home/luis/public_html/pre/teste/imagemagick/index.php(20): Imagick->writeimage('image.jpg') #1 {main} thrown in /home/luis/public_html/pre/teste/imagemagick/index.php  on line 20
============================================
Note, the CHMOD of folder is 777.

If I create another name, like otherimage.jpg (don't overwrite) the script work well.
Do you have idea why I the overwrite don't work?

Regards, JC
0
Pedro ChagasWebmasterAuthor Commented:
Hi, any idea?
Why I receive that error when I want overwrite the picture?

Regards, JC
0
karoldvlCommented:
When you open it in Imagick constructor it's locked till you release it.

Try:
$im->writeImage('other_image.jpg');
$im->destroy();
rename('other_image.jpg', 'image.jpg');

Or if this not works (Windows):
$im->writeImage('other_image.jpg');
$im->destroy();
unlink('image.jpg');
rename('other_image.jpg', 'image.jpg');
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.