Page 1 of 2

Upload form with IMagick

Posted: 2012-10-13T18:32:56-07:00
by bruno1970
Hello.
Who can help me with my upload-form?

I want to put this code into the code of my upload-form:

Code: Select all

exec("convert $original_image -thumbnail 250 250 $new_image");
Upload Form:

Code: Select all

    function checkUpload($myFILE, $file_extensions, $mime_types, $maxsize)
    {
        $errors = array();

        switch ($myFILE['error']){
            case 1: $errors[] = " ".ini_get('upload_max_filesize').".";
                    break;
            case 2: $errors[] = " ".$maxsize/(1024*1024)." ";
                    break;
                    return $errors;
                    break;
            default : break;
        }
        // MIME-Type prüfen
        if(count($mime_types)!=0 AND !in_array(strtolower($myFILE['type']), $mime_types)){
            $fehler = "Falscher MIME-Type (".$myFILE['type'].").<br />".
                      "Erlaubte Typen sind:<br />\n";
            foreach($mime_types as $type)
                $fehler .= " - ".$type."\n<br />";
            $errors[] = $fehler;
        }
        
        if($myFILE['name']=='' OR (count($file_extensions)!=0 AND !in_array(strtolower(getExtension($myFILE['name'])), $file_extensions))){
            $fehler = "Falsche Dateiendung (".getExtension($myFILE['name']).").<br />".
                      "Erlaubte Endungen sind:<br />\n";
            foreach($file_extensions as $extension)
                $fehler .= " - ".$extension."\n<br />";
            $errors[] = $fehler;
        }
        
        if($myFILE['size'] > $maxsize){
            $errors[] = " (".sprintf('%.2f',$myFILE['size']/(1024*1024))." MB).<br />".
                        " ".$maxsize/(1024*1024)." MB\n";
        }
        return $errors;
    }

    function getExtension ($filename)
    {
        if(strrpos($filename, '.'))
             return substr($filename, strrpos($filename, '.')+1);
        return false;
    }

    function getRandomValue()
    {
       #return substr(md5(rand(1, 9999)),0,8).substr(time(),-6);
		return md5(time());
		
    }

    function renameFile ($filename)
    {
        return  getRandomValue().".".getExtension($filename);
    }

    $maxsize = 2*1024*1024;
    $file_extensions = array('jpg', 'jpeg', 'jpe', 'gif', 'png');
    $mime_types = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/png', 'image/x-png');

    $ordner = "uploads/big/";

Code: Select all

elseif($_GET['act'] == 'upload'){
        
        $myFILE = $_FILES['photo'];
  
        if(count($errors)){
        // 
        }else {
            do {
	    // move and rename
            $neuer_name = renameFile($myFILE['name']);
            } while(file_exists($ordner.$neuer_name));			 
            if(@move_uploaded_file($myFILE['tmp_name'], $ordner.$neuer_name)){			
            // URL
	    echo $ordner.$neuer_name;			
            }else{
            //
            }
        }

Re: Upload form with IMagick

Posted: 2012-10-13T18:40:35-07:00
by fmw42
exec("convert $original_image -thumbnail 250 250 $new_image");
Sorry I cannot help with the Imagick conversion, but your syntax is in error. It should be

exec("convert $original_image -thumbnail 250x250 $new_image");

Re: Upload form with IMagick

Posted: 2012-10-14T01:37:37-07:00
by Bonzo
Your example and my reply are not using Imagick - they are using the comandline with exec(). Did you want to use that or Imagick?

Unless I am reading your code wrong you have multiple names for the image to be modified.

You do not need to move the file as you can add the path to the filename. Resize and move in one go.

Untested code:

Code: Select all

       $myFILE = $_FILES['photo'];
  
        if(count($errors)){
        // 
        }else {
            do {
       // move and rename
            $neuer_name = renameFile($myFILE['name']);
           $new_image = $ordner.$neuer_name;
	exec("convert $myFILE -thumbnail 250x250 $new_image");	
            } while(file_exists($new_image)); 
// Note I have had problems with file_exists() and use something like fopen.  		
            if(@fopen($new_image)){         
            // URL
       echo $new_image;         
            }else{
           echo 'Image does not exist';
            }
        }

Re: Upload form with IMagick

Posted: 2012-10-15T20:43:29-07:00
by bruno1970
Thank you.
Only this version works:

Code: Select all

do {
           // move and rename
            $neuer_name = renameFile($myFILE['name']);
			$new_image = $ordner.$neuer_name;
		   } while(file_exists($new_image));
			if(@move_uploaded_file($myFILE['tmp_name'], $ordner.$neuer_name)){
            $new_image2 = $new_image;
            exec("/usr/bin/ImageMagick_6.2.6/bin/convert $new_image -thumbnail 250x250 $new_image2");
            echo $new_image2; 			
            }else{
            echo 'Image does not exist';
            }
The Problem was the missing path to the ImageMagick Libary (shared hosting). :)
bonzo wrote:Your example and my reply are not using Imagick - they are using the comandline with exec(). Did you want to use that or Imagick?
I want to use the comandline with exec() and Imagick.

I use this upload-skript:
http://projects.keithics.com/ajaxcrop/demo.php

The problem is that i want to use the thumbnail as a setting copy with minimum 300dpi but the GDlib resize the orginal to 72dpi.
The second problem is that i can not add a border to the image with the GDlib.

This is the last part of my skipt:

Code: Select all

if($_GET['act'] == 'thumb'){

	$arr = array(
	'uploaddir' 	=> 'uploads/thumb/',
	'tempdir'		=> 'uploads/temp/',
	'height'		=> $_POST['height'],
	'width'		=> $_POST['width'],
	'x'			=> $_POST['x'],
	'y'			=> $_POST['y'],
	'img_src'		=> $_POST['img_src'],
	'thumb'		=> true
	);

$uploaddir  = $arr['uploaddir']; # uploads/thumb/
$tempdir    = $arr['tempdir'];   # uploads/temp/	
$orginal     = $arr['img_src'];    #EXAMPLE uploads/big/44c8168a8a8cd7117f891dd.jpg

//Get the new coordinates to crop the image.
$height		= $arr['height'];    
$width		= $arr['width'];
$x			= $arr['x'];
$y			= $arr['y'];	

####CROP####

exec("/usr/bin/ImageMagick_6.2.6/bin/convert $orginal -crop $height x $width+$x+$y $thumbnail");

####RESIZE THE THUMBNAIL TO 450x450####

exec("/usr/bin/ImageMagick_6.2.6/bin/convert $orginal -resize 450x450 $thumbnail");

####ADD BORDER 50x50####	

exec("/usr/bin/ImageMagick_6.2.6/bin/convert $orginal -border 50x50 $thumbnail");
	
####ADD FULLSIZED WATERMARK-IMAGE (Transparent PNG 500x500)####	
	
exec("/usr/bin/ImageMagick_6.2.6/bin/convert $orginal -watermark watermark.png $thumbnail");
	
####SAVE THE IMAGE TO $uploaddir with the same name as the orginal####
	
echo $thumbnail;
}


How can i do that?

Re: Upload form with IMagick

Posted: 2012-10-15T20:57:38-07:00
by fmw42
You are using an ancient version of IM. Version 6.2.6 is 540 versions old. I am not sure that
convert $orginal -watermark watermark.png $thumbnail"
is not valid even for that old version. see

http://www.imagemagick.org/Usage/compose/#watermark

The convert version uses -compose modulate as is newer than your IM version (it requires at least 6.5.3-4). You need to use the composite form.

But you should be able to do the crop and watermark within Imagick commands, unless your version of IM is too old for Imagick.

It probably would be better to do it all with exec() or all with Imagick rather than split the code. Personally, I would not rely upon Imagick especially with such an old version of Imagemagick. Furthermore, Imagick is not being updated/maintained to the level of new features within Imagemagick. But I suppose with such an old version of Imagemagick, it does not matter.

Also when you use -crop, you should follow it with +repage.

See
http://www.imagemagick.org/Usage/crop/#crop_repage

Also
convert $orginal -crop $height x $width+$x+$y
is not valid with the spaces around the x. It should be $heightx$width+$x+$y.

Also you seem to be doing each step on $original not the previous version of your processing. So you are overriding each $thumbnail, such that it does not have all the previous processing. If that is what you want the fine. Otherwise all the convert commands can be processed in one command and you do not need to have separate convert commands. Or if you want separate commands, then change the $original to $thumbnail for the input images after the crop command.

Re: Upload form with IMagick

Posted: 2012-10-15T21:50:25-07:00
by bruno1970
fmw42 wrote:You are using an ancient version of IM. Version 6.2.6 is 540 versions old
I have checked that. My hoster has now a newer version.
phpinfo wrote:imagick module version: 3.0.1
imagick classes: Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version: ImageMagick 6.5.7-8 2012-08-17 Q16 http://www.imagemagick.org
ImageMagick release date: 2012-08-17
fmw42 wrote: I am not sure that
convert $orginal -watermark watermark.png $thumbnail"
is not valid even for that old version.

O.K. I will improve that.
fmw42 wrote: Also when you use -crop, you should follow it with +repage.
See http://www.imagemagick.org/Usage/crop/#crop_repage
I dont want to show the thumbnail on my site. I only want to create and save it.
fmw42 wrote:Also
convert $orginal -crop $height x $width+$x+$y
is not valid with the spaces around the x. It should be $heightx$width+$x+$y.
I have got an error like "invalid var $heightx " if i write "$heightx$width".
fmw42 wrote: Otherwise all the convert commands can be processed in one command and you do not need to have separate convert commands. Or if you want separate commands, then change the $original to $thumbnail for the input images after the crop command.

Code: Select all

//Get the new coordinates to crop the image.
$height      = $arr['height'];   
$width      = $arr['width'];
$x         = $arr['x'];
$y         = $arr['y'];   

####CROP####

exec("/usr/bin/ImageMagick_6.5.7-8/bin/convert $orginal -crop $heightx$width+$x+$y $thumbnail");

####RESIZE THE THUMBNAIL TO 450x450 | ADD BORDER 50x50 | ADD FULLSIZED WATERMARK-IMAGE####

exec("/usr/bin/ImageMagick_6.5.7-8/bin/convert $thumbnail -resize 450x450 -border 50x50 composite -watermark watermark.png $thumbnail2");

echo $thumbnail2;
}
Is that correct?

Re: Upload form with IMagick

Posted: 2012-10-15T21:56:06-07:00
by fmw42
Also when you use -crop, you should follow it with +repage.
See http://www.imagemagick.org/Usage/crop/#crop_repage
You still need to remove the virtual canvas so other subsequent processing does not get messed up.
$heightx$width+$x+$y
Sorry, it should be ${height}x${width}+$x+$y to avoid the $heightx issue. (or use $height,$width+$x+$y, which I think also works)

convert $thumbnail -resize 450x450 -border 50x50 composite -watermark watermark.png $thumbnail2
You cannot combine convert and composite in the same command. In this case you must use two separate commands. One starting with convert and the latter starting with composite for the watermark.

Do the -crop, -resize, -border in one convert command and the follow that with composite for the watermark. Or use the convert version of -watermark which is -compose modulate as per:

http://www.imagemagick.org/Usage/compose/#watermark

Then all your commands can be in one convert statement.


P.S. There is also a newer version of Imagick. See http://pecl.php.net/package/imagick/3.1.0RC2

Re: Upload form with IMagick

Posted: 2012-10-15T22:18:36-07:00
by bruno1970
fmw42 wrote: You cannot combine convert and composite in the same command. In this case you must use two separate commands. One starting with convert and the latter starting with composite for the watermark.
Is that correct?:

Code: Select all

if($_GET['act'] == 'thumb'){

	$arr = array(
	'uploaddir' 	=> 'uploads/thumb/',
	'tempdir'		=> 'uploads/temp/',
	'height'		=> $_POST['height'],
	'width'			=> $_POST['width'],
	'x'				=> $_POST['x'],
	'y'				=> $_POST['y'],
	'img_src'		=> $_POST['img_src'],
	'thumb'			=> true
	);
	
$uploaddir  = $arr['uploaddir']; # uploads/thumb/
$tempdir    = $arr['tempdir'];	 # uploads/temp/	
$orginal    = $arr['img_src'];   #EXAMPLE uploads/big/44c8168a8a8cd7117f891dd.jpg
//Get the new coordinates to crop the image.
$height      = $arr['height'];   
$width      = $arr['width'];
$x         = $arr['x'];
$y         = $arr['y'];   

####CROP | RESIZE THE THUMBNAIL TO 450x450 | ADD BORDER 50x50####
exec("/usr/bin/ImageMagick_6.5.7-8/bin/convert $orginal -crop ${height}x${width}+$x+$y -resize 450x450 -border 50x50 $thumbnail");

####ADD FULLSIZED WATERMARK-IMAGE####
exec("/usr/bin/ImageMagick_6.5.7-8/bin/convert -size 450x450 $thumbnail watermark.png");

echo $thumbnail;

}
fmw42 wrote:P.S. There is also a newer version of Imagick. See http://pecl.php.net/package/imagick/3.1.0RC2
My website is on a shared hoster. I have to take what is there but i will check out if is possible to get a newer version.

Re: Upload form with IMagick

Posted: 2012-10-16T10:23:22-07:00
by fmw42
convert $orginal -crop ${height}x${width}+$x+$y -resize 450x450 -border 50x50 $thumbnail
You should properly include +repage after the crop

convert -size 450x450 $thumbnail watermark.png
I am not sure what you are doing here? -size 450x450 is doing nothing. If you are trying to resize the thumbnail to a watermark image, then you need -resize 450x450 after the reading of $thumbnail.

If you are trying to add a watermark to the thumbnail image, you need to have a second black/white image to overlay on the thumbnail. Please read carefully the page on watermark composite or -compose modulate at http://www.imagemagick.org/Usage/compose/#watermark

Re: Upload form with IMagick

Posted: 2012-10-16T11:15:19-07:00
by Bonzo
I want to use the comandline with exec() and Imagick.
I hate to labour a point but due to the choice of the Imagick class name it causes confusion.
You are using "comandline Imagemagick with exec()" NOT Imagick at all. See http://php.net/manual/en/class.imagick.php

I also put the $ into the curly brackets:

Code: Select all

-crop {$height}x{$width}+$x+$y
As fmw42 says you need the +repage to remove the vertual canvas.

-watermark works with composite and not convert:

Code: Select all

<?php 
// The watermak to add possibly with a transparent background
$input1 = 'watermark.png';
// The image to add a watermark to
$input = 'image.jpg';
$cmd = "-watermark 80% -gravity center $input1 $input";  
exec("composite $cmd watermark.jpg"); 
 ?> 
What result are you actualy after an example would help even from another graphics program.

You can see my website linked below for example of php and Imagemagick use with some Imagick code as well.
There are also some watermaking examples here: http://www.rubblewebs.co.uk/imagemagick/watermark.php

Re: Upload form with IMagick

Posted: 2012-10-16T11:40:46-07:00
by fmw42
As fmw42 says you need the +repage to remove the vertual canvas.
Clarification. This is really only needed if the immediate output from the crop command is PNG, GIF, MIFF, MPC, TIFF?. It is not needed if the output format does not keep virtual canvas information, such a JPG.
-watermark works with composite and not convert:
True, But for relatively current versions of IM, you can use an equivalent convert command with -compose modulate to do the same as composite -wartermark.

see
http://www.imagemagick.org/Usage/compose/#watermark

That would allow all the processing he needs to be done in one IM convert command. Though it is perfectly fine to separate them into multiple commands as appropriate to the specific processing to be done.

P.S. Thanks Bonzo for the PHP exec() watermark code example.

Re: Upload form with IMagick

Posted: 2012-10-16T12:15:09-07:00
by bruno1970
Bonzo wrote: You are using "comandline Imagemagick with exec()" NOT Imagick at all.
O.K. I will never write Imagick again. :)
Bonzo wrote: I also put the $ into the curly brackets:

Code: Select all

-crop {$height}x{$width}+$x+$y
That was fmw42s idea... ;)
-watermark works with composite and not convert:
I have found the convert-version here: http://www.imagemagick.org/Usage/compose/#compose

Re: Upload form with IMagick

Posted: 2012-10-16T14:44:53-07:00
by fmw42
Actually the form I use is:

-crop ${height}x${width}+$x+$y

with the $ outside the braces

Re: Upload form with IMagick

Posted: 2012-10-16T15:47:13-07:00
by bruno1970
fmw42 wrote:Actually the form I use is:

-crop ${height}x${width}+$x+$y

with the $ outside the braces
I can not test it because i only get errors with my code or i see nothing. :?
Can you tell me please how i have to change the code?

Code: Select all

if($_GET['act'] == 'thumb'){

	$arr = array(
	'uploaddir' 	=> 'uploads/thumb/',
	'tempdir'		=> 'uploads/temp/',
	'height'		=> $_POST['height'],
	'width'			=> $_POST['width'],
	'x'				=> $_POST['x'],
	'y'				=> $_POST['y'],
	'img_src'		=> $_POST['img_src'],
	'thumb'			=> true
	);

$uploaddir  = $arr['uploaddir']; # uploads/thumb/
$tempdir    = $arr['tempdir'];	 # uploads/temp/	
$orginal    = $arr['img_src'];   #EXAMPLE uploads/big/44c8168a8a8cd7117f891dd.jpg
//Get the new coordinates to crop the image.
$height      = $arr['height'];   
$width      = $arr['width'];
$x         = $arr['x'];
$y         = $arr['y'];   

$new_image = $arr['img_src'];
####CROP | RESIZE THE THUMBNAIL TO 450x450 | ADD BORDER 50x50####
exec("/usr/bin/ImageMagick_6.2.6/bin/convert $new_image -crop {$height}x{$width}+$x+$y -resize 450x450 -border 50x50 $uploaddir.$thumbnail");

####ADD FULLSIZED WATERMARK-IMAGE####
exec("/usr/bin/ImageMagick_6.2.6/bin/convert -size 450x450 $uploaddir.$thumbnail watermark.png");

echo $thumbnail; 
}

Re: Upload form with IMagick

Posted: 2012-10-16T17:20:24-07:00
by fmw42
exec("/usr/bin/ImageMagick_6.2.6/bin/convert -size 450x450 $uploaddir.$thumbnail watermark.png");
This line is totally incorrect. Furthermore, you have not defined an input image to use for adding the watermarking to your original or thumbnail

See the compose reference page for watermark/modulate or use Bonzo's example. He is more versed with PHP that I.