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 3
Pedro ChagasWebmasterAsked:
Who is Participating?
 
blue-devConnect With a Mentor Commented:
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
 
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
 
karoldvlConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.