Convert using ImageMagick using PHP

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Convert using ImageMagick using PHP

Post by jaykappy »

I have an image upload test web page. I am using PHP to upload images to webserver folders.
Along the way I am looking to create a thumbnail and save it in a specific location.

Has anyone done that using ImageMagick? If so any examples on the PHP syntax?

Currently I am simply converting a multipage pdf to png using - convert image.pdf[0] image.png

BUT in my PHP I would be working with varaibles so would need something like the below...I have the syntax all messed up...

' 'convert ', .'$image_id'. '.pdf[0] ', .'$image_id'.'.png'

Has anyone done this before? Any help would be greatly appreciated. Thanks
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert using ImageMagick using PHP

Post by Bonzo »

A good way to write the code when using variables with php and when you have a longer command is this:

Code: Select all

$cmd = " $input_image operator ".
 " operator $output_image";
exec("convert $cmd ");
When writing like this you can echo $cmd to see what the actual code Imagemagic is going to run. I think it also makes the code easier to read.

Again I would construct the image path outside the imagemagick command:

Code: Select all

$input = $image_id' . '.pdf[0];
$output = $image_id' . '.png'
$cmd = "  $input -resize 50x50 $output ";
// Un coment the line below if you have problems
// echo $cmd.'<br>';
exec(" convert $cmd ");
The code is a bit longer but debugging is simpler.

Some php and Imagemagick information on my site - link in my signature.

P.S.
I forgot your original question; a basic example of image uploading with a resize. Make sure you have some file validation!
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

Yea I forgot to ask about that.....I would need to add that to the image input and image output..

There is an 'images' folder at root of website project
I am creating a folder for the image based off of an $album_id, this number is passed to the Function that is running this code
I then grab the $image_id that was just given to the uploaded image and then add the extension (png)
So my path would be such...

images / $album_id / $image_name .png

Code: Select all

$input = 'Images/' . $album_id . '/' . $image_id . '.pdf[0];
$output = 'Images/Thumbs/' . $album_id . '/' .$image_id . '.png;
Can you glance at that syntax for the imput and output....

Really Appreciate your help and comments....
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert using ImageMagick using PHP

Post by Bonzo »

Try:

Code: Select all

$input = '/Images/' . $album_id . '/' . $image_id . '.pdf[0]';
$output = '/Images/Thumbs/' . $album_id . '/' .$image_id . '.png';
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

I am doing this:
the PDF image is being added to the MySQL database
the PDF image is beign moved to the apprpriate folder
But no thumbnail png is being created at the appropriate location

No image is being created and put in the proper folder....
The MySQL server is on a different server...the Web Pages and IMage OFlders are all local to my machine.
I have a Thumbnail Function that doe simage files (except pdf) that was working so I know I can write to the files...

This is the Echo I am getting This echo looks right to me...altough I tried it without the / in front as well...no luck either.
convert /uploads/27/17267.pdf -resize 50x50 /uploads/thumbs/27/17267.png

convert uploads/27/17269.pdf -resize 50x50 uploads/thumbs/27/17269.png

The uploads/27 folders exist as the PDF image is being moved there successfully
The uploads/27/thumbs folder exists as well.

Any thoughts? Maybe the webpage does not have access to ImageMagick?

Code: Select all

			
$album_id = (int)$album_id;
mysql_query("INSERT INTO images (user_id, album_id, timestamp, ext, description, name) VALUES ('".$_SESSION['user_id']."', '$album_id', UNIX_TIMESTAMP(), '$image_ext', '$image_desc', '$imagename')");

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);

$input = '/uploads/' . $album_id . '/' . $image_file;
$output = '/uploads/thumbs/' . $album_id . '/' .$image_id . '.png';

$cmd = " $input -resize 50x50 $output ";	
echo $cmd.'<br>';
exec("convert $cmd");
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

I even tried to hard code it and nothing was created.

echo - uploads/27/17280.pdf uploads/thumbs/27/test.png

Code: Select all

			
$input = 'uploads/27/17280.pdf';
$output = 'uploads/thumbs/27/test.png';
			
$cmd = "$input $output";

echo $cmd.'<br>';

exec("convert $cmd");
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert using ImageMagick using PHP

Post by Bonzo »

1/ As you are using a variable you need " " not ' '
2/ Have you CHMOD the thumbs folder to 755 or 777 depending on your server setup
3/ Try changing this line:

Code: Select all

exec('convert $cmd');
To this:

Code: Select all

$array=array(); 
echo "<pre>"; 
exec("convert  $cmd 2>&1", $array);  
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";  
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

Not really sure about number 2 and what that means, nor where I would do that
2/ Have you CHMOD the thumbs folder to 755 or 777 depending on your server setup (currently I am using IIS , but in test/dev environment I at home I am using Apache) So I guess I need to know whats going on with both servers and how to remedy this issue. Any thoughts woudl be much appreciated.

Right now I can do this with a JPG, PNG etc as PHP has a convert for that....so I know that I can get Thumbs created for those images....I thought I set the 755 but cannot remember where...


Thanks again for your help....THis is what I get with the code you posted

MY CODE:

$input = '/uploads/27/17280.pdf';
$output = '/uploads/thumbs/27/test.png';

$cmd = "$input $output";
echo $cmd.'<br>';

$array=array();
echo "<pre>";
exec("convert $cmd 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";

RESULT:

/uploads/27/17280.pdf /uploads/thumbs/27/test.png
Array
(
[0] => Invalid Parameter - /27
)

1
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

I tried this and got this error: just trying to hard code it for testing purposes

CODE:

Code: Select all

$input = chmod("/uploads/Test/17281.pdf", 0755);
$output = chmod("/uploads/thumbs/TEST/test.png", 0755);
			
$cmd = "$input $output";
echo $cmd.'<br>';
			
$array=array(); 
echo "<pre>"; 
exec("convert  $cmd 2>&1", $array);  
echo "<br>".print_r($array)."<br>"; 
echo "</pre>"; 
RESULT:

Code: Select all

Array
(
    [0] => Must specify a file system
)

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

Re: Convert using ImageMagick using PHP

Post by Bonzo »

I would try putting your pdf file and output file in the same directory and see if it works.

CHMOD -> the folders need to be writable and so you change the permissions of them in your FTP program.

Code: Select all

$input = "17281.pdf";
$output = "test.png";
         
$cmd = "$input $output";
echo $cmd.'<br>';
         
$array=array(); 
echo "<pre>"; 
exec("convert  $cmd 2>&1", $array);  
echo "<br>".print_r($array)."<br>"; 
echo "</pre>"; 
Right now I can do this with a JPG, PNG etc as PHP has a convert for that
So the code works OK with converting image files but not pdf files? Do you know if you have Ghostscript installed as you need that to work with pdf files.
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

I was uploadubg images with this, and it was creating thumbnail images fine....albeit with jpg, png, etc...

Code: Select all

$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$image_ext;
move_uploaded_file($image_temp, 'uploads/'.$album_id.'/'.$image_file);
Thumbnail('uploads/'.$album_id.'/', $image_file, 'uploads/thumbs/'.$album_id.'/');

But PHP does not convert PDF so thats why I installed ImageMagick...THATS all I installed, I though Ghost Script was included.
I only installed IMageMagick on the server. DID NOTHING Else and running this code

Code: Select all

	$input = chmod("156.142.2.31:84/inetpub/wwwroot/ImageUpload/uploads/Test/17281.pdf[0]", 0755);
	$output = chmod("156.142.2.31:84/inetpub/wwwroot/ImageUpload/uploads/thumbs/TEST/test.png", 0755);

	$cmd = "$input $output";
	echo $cmd.'<br>';
			
	$array=array(); 
	echo "<pre>"; 
	exec("convert  $cmd 2>&1", $array);  
	echo "<br>".print_r($array)."<br>"; 
	echo "</pre>"; 

I am very green and am thankful for your patience....what other settings do I need to set on the server or in code to make this happen


CURRENT ERROR:

Code: Select all

Array
(
    [0] => Must specify a file system
)

1

THANK YOU FOR YOUR PATIENCE AND HELP....VERY APPRECIATED...
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert using ImageMagick using PHP

Post by Bonzo »

Are you on a Windows or Linux server?

That's OK installing Imagemagick can be confusing.
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

I am currently testing on IIS windows server....but i run an apache server at home...
Currently trying to get this working in IIS...do I have to set some settings in IIS manager?

Imagemagick comes with Ghostscript right? I shoudl be good there?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Convert using ImageMagick using PHP

Post by Bonzo »

The Windows part may be your problem; I know on Windows operating systems there is a file called convert to rename files? exec( ) may be trying to use that instead of the Imagemagick convert.

You will need to find the path to convert and I am not sure how to do that on a Windows server. On Linux it would be something like usr/local/bin/convert

I did not install Ghostscript on my server but that is a Linux one and probably had Ghostscript installed already.

I would assume you need to install Ghostscript and use the path to convert in your command.
jaykappy
Posts: 41
Joined: 2013-07-12T07:58:15-07:00
Authentication code: 6789

Re: Convert using ImageMagick using PHP

Post by jaykappy »

THis is the path to my convert.exe C:\Program Files\ImageMagick-6.8.6-Q16\convert.exe

is this what you are refering too?

If I go to CMD prompt I can convert fine...so I dont think that convert is the issue, but agree it might be trying to use the windows convert instead of imagemagick convert....If I have to path to the .exe above how to I specify that in my code?
Post Reply