Install and use ImageMagick with PHP

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
YBthebest

Install and use ImageMagick with PHP

Post by YBthebest »

Hello! The title says it all...I found a tutorial that uses ImageMagick, and I would want to use it! I searched about it, but I just can't find the solution...

1.
exec("/usr/local/bin/convert /home/cra/cap.jpg -quality 100 /home/cra/cap.jpg");
2.
//optimize
3.
exec("/usr/local/bin/convert /home/cra/cap.jpg -fuzz 25000 -fill black -draw ‘color 5,5 floodfill’ -quality 100 /home/cra/cap_c.jpg");
4.
//floodfill and fill bg in black
5.
exec("/usr/local/bin/convert /home/cra/cap_c.jpg -negate -quality 100 /home/cra/cap_h.jpg");
6.
//negate the picture
7.

8.
exec("/usr/local/bin/convert /home/cra/cap_h.jpg -shave 10×10 -quality 100 /home/cra/cap_cra.jpg");
9.
//remove the border


This is the code...how can I use it?

Thank you very much for the help :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

Is imagemagick installed:

Code: Select all

<?php 
echo "<pre>"; 
system("convert -version");   
echo "</pre>"; 
?> 
What is the path to imagemagick:

Code: Select all

<?php 
echo "<pre>"; 
system("type convert");  
echo "</pre>"; 
?> 
OR
<?php 
echo "<pre>"; 
system('which convert',$path); print_r($path);  
echo "</pre>"; 
?> 
If the above is OK just run your code
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Version: ImageMagick 6.6.0-4 2010-08-11 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP

Seams it's installed :)

/usr/bin/convert

and that's the path...so yeah, seems all right :)

Thanks man :) really, thanks
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

Thats Ok; you may be able to just use convert rather than usr/bin/convert I also use relative paths.
Do not forget to CHMOD the folder you are saving the images into to 755 or 777

Do not save intermediate images as jpg as they get compressed and the code you posted can be improved - if you have problems check my examples or post info on what you are trying to do.
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Hey, you just posted after my 2nd post so I assume you didn't see it...as I said, I'm a bit lost with the folders!

And I don't understand, why can't I save it as .jpg?

Thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

jpg is a lossy compression - everytime you save an intermediate image; one you are going to edit again as in your example code - you lose some detail. In that case save the imtermediate images as something like a png which is not compressed.

If you use relative paths they are in relation to where your code is.
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Ok I understand!

But I still don't get the folder thing, wth is the usr bin convert thing? I can't find it! and the output folder I can just put: image/image.png?

Thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

Do not worry about the usr/bin/convert that is the path to Imagemagick.

Start simple just put your code in a folder along with your image ( e.g. called input.jpg ) and save to the same folder - CHMOD the folder to 755 or 777.

Code: Select all

<?php
exec("/usr/bin/convert input.jpg -bordercolor black -border 10x10 output.jpg");
?>
<img src="output.jpg">
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Wooooow! I love ya man ! Thank you sooo much!


EDIT: There is just one thing! It gives me a black background and white letters and shapes, while it should be white background and black letters and shapes!

Is that normal? Thanks!
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

I don't really understand, I'm trying too but...it's difficult :P
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

The code you posted is a bit of a mess do you have an example or description of what you are trying to do?
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Hello!

So I managed to make the code work !

<?php
exec("/usr/bin/convert image.jpg -quality 100 image.jpg");
//optimize

exec("/usr/bin/convert image.jpg -fuzz 25000 -fill black -draw ‘color 5,5 floodfill’ -quality 100 image_c.jpg");

//floodfill and fill bg in black

exec("/usr/bin/convert image_c.jpg -negate -quality 100 image_h.jpg");
//negate the picture
exec("/usr/bin/convert image_h.jpg -shave 11×14 -quality 100 image_cracked.jpg");

?>



So, this is my basis image:
http://ybthebest.com/test/image.jpg

And this is my final image:
http://ybthebest.com/test/image_final.jpg

But as you see, there is still that annoying background with the white lines on it...how can I get rid of that and ONLY have the 2 numbers and the sign?

Thanks !
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

Is this a CAPTCHA on someone elses website you are trying to crack ?
YBthebest

Re: Install and use ImageMagick with PHP

Post by YBthebest »

Hello!

Thanks for your answer!
Nope, I'm not trying to crack someone elses captcha. Some people asked me to input a captcha on their website, and I wanted to make it myself (I can give you the code if you want !)

And I want to test if it is crackable, and how it works. ImageMagick and gOCR interest me very much, so...

Thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Install and use ImageMagick with PHP

Post by Bonzo »

OK try these two - one will give you white text on a black background and the other black text on a white background:

Code: Select all

exec("/usr/bin/convert image.jpg -negate -black-threshold 30% -trim 3image_c.jpg");

exec("/usr/bin/convert image.jpg -white-threshold 70% -trim 4image_c.jpg");
Post Reply