In my web hosting, I can't use shell, so is there any alternative for this command?
system("convert $src +dither -layers optimize -depth 16 -colors 32 $dest");
I want to compress gif files, so I need to preserve animation.
P.S:
Of course, that I first tried to do it by my own. But without success. So i wrote this question.
Example:
$im = new Imagick("first.gif");
$im->optimizeImageLayers();
$im->writeImages("phpmodul.gif", true);
system("convert first.gif -layers optimize shell.gif");
first.gif - 649 kB
phpmodul.gif - 647 kB
shell.gif - 347 kB
Any help?
P.P.S:
I solved this. My solution.
$animation = new Imagick("$file");
foreach ($animation as $frame) {
$frame->quantizeImage(32, imagick::COLORSPACE_RGB, 16, FALSE, TRUE);
}
$animation = $animation->optimizeImageLayers();
$animation->writeImages("$file", true);
Related
I need to convert the ImageMagick Command to PHP Imagick. I have tried a few combinations but nothing worked as expected. The command creates a line with pointy edges and gradient at the edges
The command is:
convert -size 300x1 xc:red \
\( -size 1x300 gradient: -rotate 90 -solarize 50% -level 0x50% -white-threshold 50% +write grad.png \) \
-alpha off -compose copy_opacity -composite red_grad.png`
thanks to #fmw42 for this command
I have tried the following code:
$line = new Imagick();
$line->newPseudoImage(300,1,'xc:red');
$shadow = new Imagick();
$shadow->newPseudoImage(1, 300, 'gradient:red-white');
$shadow->rotateImage('transparent', 90);
$shadow->solarizeImage(50);
$shadow->levelImage(0,50,50);
$shadow->whiteThresholdImage('white');
$shadow->setImageCompose(0);
$shadow->writeImage('grad.png');
$shadow->compositeImage($line, Imagick::COMPOSITE_MATHEMATICS, 0, 0);
Please point out where am I going wrong
My best guess at translation would be the following. However, this is untested and I do not know Imagick well.
$redline = new Imagick();
$redline->newPseudoImage(300,1,'xc:red');
$grad = new Imagick();
$grad->newPseudoImage(1, 300, 'gradient:black-white');
$grad->rotateImage('white', 90);
$grad->solarizeImage(50*Imagick::getQuantumRange/100);
$grad->levelImage(0,1,50*Imagick::getQuantumRange/100);
$grad->whiteThresholdImage(50*Imagick::getQuantumRange/100);
$redline->compositeImage($grad, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$redline->writeImage("redline.png");
With a few tweaks, #fmw42 answer worked for me. The correct solution is given below
$redline = new Imagick();
$redline->newPseudoImage(1100,3,'xc:'.$chipColourPixel->getColorAsString());
$grad = new Imagick();
$grad->newPseudoImage(3, 900, 'gradient:black-white');
$grad->rotateImage('white', 90);
$grad->solarizeImage((int)ceil(50*Imagick::QUANTUM_RANGE/100));
$grad->levelImage(0,1,50*Imagick::QUANTUM_RANGE/100);
$redline->compositeImage($grad, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$redline->writeImage("redline.png");
I need to create square images with no image loss. I found a tool that does the job as a bash script using ImageMagick but can never seem to be able to do it with php Imagick.
The script I found is called squareup from http://www.fmwconcepts.com/imagemagick/squareup/index.php
My code looks like this currently:
$image = new Imagick($srcimage);
$image->setCompressionQuality(100);
if ($image->getImageHeight() <= $image->getImageWidth())
$image->resizeImage($maxsize, 0, Imagick::FILTER_MITCHELL, 1);
else
$image->resizeImage(0, $maxsize, Imagick::FILTER_MITCHELL, 1);
$h=$image->getImageHeight();
$w=$image->getimagewidth();
$hlarge=0;
$wlarge=0;
if ($w>$h) {
$diff=intval(($w-$h)/2);
$wlarge=1;
$h=$w;
} else {
$diff=intval(($h-$w)/2);
$w=$h;
$hlarge=1;
}
$newimage = new Imagick();
if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
$fg="cmyk(0,0,0,0)";
$fg_pixel=new ImagickPixel($fg);
$newimage->newImage($w, $h, $fg_pixel);
$newimage->setImageColorspace(Imagick::COLORSPACE_CMYK);
} else {
$newimage->newImage($w, $h, new ImagickPixel('#ffffff'));
}
$newimage->compositeImage($image,\Imagick::COMPOSITE_OVER,0,0);
$newimage->setImageCompression(Imagick::COMPRESSION_JPEG);
$newimage->setImageCompressionQuality(100);
$newimage->stripImage();
$newimage->writeImage($contactimage);
$newimage->destroy();
$image->destroy();
The simplest way to do pad to square or crop to square in ImageMagick 6 is as follows:
Input:
size=`convert hatching_orig.jpg -format "%[fx:max(w,h)]" info:`
convert hatching_orig.jpg -background red -gravity center -extent ${size}x${size} hatching_pad.jpg
size=`convert hatching_orig.jpg -format "%[fx:min(w,h)]" info:`
convert hatching_orig.jpg -background red -gravity center -extent ${size}x${size} hatching_crop.jpg
Same command, but different size variable.
In IM 7, you can do each in one command line.
These commands should be easy to convert to Imagick, I would expect. But should be done in sRGB colorspace. See http://us3.php.net/manual/en/imagick.extentimage.php
In php using Imagick, I'm able to convert one pdf page into a jpg image at once. But I need to convert all pages of my pdf into jpg files in separate folder.
below my code
<?php
for($i=0;$i<=20;$i++){
$pdf_file = 'book.pdf';
$save_to = 'pages/tw'.$i.'.jpg';
$img = new imagick();
$img->setResolution(200,200);
$img->readImage("{$pdf_file}[$i]");
$img->scaleImage(800,0);
$img->setImageFormat('jpg');
$img = $img->flattenImages();
$img->writeImages($save_to, false);
$img->destroy();
}
?>
Above code produces results up-to 10 pages. Then it terminated by execution time of 30 secs. I cant manage php.ini because I'm using hosting with another company.
$mypdf = escapeshellarg( "mysafepdf.pdf" );
$newjpg = escapeshellarg( "output.jpg" );
$result = 0;
exec("convert -density 600 {$mypdf} {$newjpg} -colorspace RGB -resample 300", null, $result);
$ result will be 0 if the conversion works
-density = dpi
I hope this will help you!
PS.: This is only for one image, but you can adapt at it for your $i.
I have been working on converting PDFs to JPGs, for this I have installed imagick and GhostScript. I have been using exec() in my php code to make the conversion. Now my problem is that if the source of the input pdf is a conversion from doc->pdf, then the image quality is grainy when zoomed. On the other hand I need to keep the image size below 500kb, so I cannot use
density more than 200.
Is there a way to add any sort of filter before saving the images, so that the jpg quality is improved.
Here is my sample code:
$inputFileName = 'test.pdf';
$outputFileName = 'converted.jpg';
$sourceFile = escapeshellarg( $inputFileName );
$outputFile = escapeshellarg( $outputFileName );
$exe = "convert -density 200 -colorspace RGB {$sourceFile } {$outputFile }";
$null = "0";
echo exec( $exe, $null, $result );
Any help would be appreciated!
Thanks
Why not increasing the density and decreasing the quality? For example:
$exe = "convert -density 600 -quality 70 -colorspace RGB {$sourceFile } {$outputFile }";
Well how could I change the before image to the after image by using imagemagick?
Is it the -skew command or the -distort, and how can I use it preferably in typo3 and php?
Any help is appreciated!
Using Imagemagick with php and the command line:
// Working on the original image size of 400 x 300
$cmd = "before.jpg -matte -virtual-pixel transparent".
" +distort Perspective \"0,0 0,0 400,0 400,22 400,300 400,320 0,300 0,300 \" ";
exec("convert $cmd perspective.png");
Note:
1/ This is for later versions of Imagemagick - the perspective operator has change.
2/ You need to use +distort not -distort as the image is larger than the initial image boundrys.
Examples of Imagemagick with php usage on my site http://www.rubblewebs.co.uk/imagemagick/operator.php
I think what you're looking for is the Imagick::shearImage function. This creates a checkerboard square and distorts it into a parallelogram (save this as a PHP file and open in your browser to see):
<?php
$im = new Imagick();
$im->newPseudoImage(300, 300, "pattern:checkerboard");
$im->setImageFormat('png');
$im->shearImage("transparent", 0, 10);
header("Content-Type: image/png");
echo $im;
?>
Within a larger script, to shear an image named myimg.png and save it as myimg-sheared.png, you can use:
$im = new Imagick("myimg.png");
$im->shearImage("transparent", 0, 10);
$im->writeImage("myimg_sheared.png");
If shearImage isn't versatile enough, you can try the Imagick::DISTORTION_PERSPECTIVE method via the Imagick::distortImage function.
Perspective distortion should give you what you want. Example:
convert original.png -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5' distorted.png
In TYPO3 you could apply it by (ab)using the SCALE object of the GIFBUILDER. Example:
temp.example = IMAGE
temp.example {
file = GIFBUILDER
file {
format = jpg
quality = 100
maxWidth = 9999
maxHeight = 9999
XY = [10.w],[10.h]
10 = IMAGE
10.file = fileadmin/original.png
20 = SCALE
20 {
params = -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5'
}
}
}