ImageMagick exec commands to reduce noise on white background to PHP Imagick - php

I need this line:
magick "input.jpg" -fuzz 4% -transparent white -background white -alpha remove -alpha off -quality 85% "result.jpg"
to PHP imagick:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
// ???
$img->setImageFormat('jpg');
$img->setImageCompressionQuality(85);
$img->writeImage($root . '/testall/imagemagick/result.jpg');
and I can't find fuzz in https://www.php.net/manual/de/book.imagick.php to even start with.
Please help

You might be able to use: Imagick::transparentPaintImage:
$img = new Imagick($root . '/testall/imagemagick/input.jpg');
$fuzz = 4 * \Imagick::getQuantum(); // Copied from the example?
$img->transparentPaintImage('white', 0, $fuzz, false);
...

Well I tried around with andlrc answer and this is the working result:
$img = new Imagick('input.jpg');
$fuzz = Imagick::getQuantum() * ($whitePercentage / 100);
$BackgroundColor = "rgb(255, 255, 255)";
$img->setImageFormat('png');
$img->transparentPaintImage($BackgroundColor, 0, $fuzz, false);
$img->setimagebackgroundcolor('white');
$img = $img->mergeImageLayers( imagick::LAYERMETHOD_FLATTEN );
$img->setImageFormat('jpg');
$img->setImageCompressionQuality($qualy);
$img->writeImage($root . '/testall/imagemagick/result.jpg');

Related

Allow all special characters using imagemagick

I'm using Imagemagik in PHP codeigniter, I want to allow all special character, smiley etc.. but it's not working. Can anyone help me how to do this. Here is my code.
$textonimage = $this->input->post('textonimage');
if(!empty($background)){
$string = $textonimage;
$smart_wordwrap_info = smart_wordwrap($string, 20);
$height_1 = substr_count( $smart_wordwrap_info, "\n" );
$color_string_length = strlen($colors);
if($color_string_length > 7){
$colors_result = substr($colors, 3);
$colors = '#'.$colors_result;
}
$height_2 = $height_1+1;
$height_3 = $height_2-2;
$height_4 = 30*$height_3;
$height = 200+$height_4;
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( $colors );
$image->newImage(350, $height, $pixel);
$image->setImageFormat('png');
$image->writeImage(FCPATH.'uploads/post-image/my_output.png');
$process_file_path = FCPATH.'uploads/post-image/my_output.png';
$target_file_name = mt_rand(10000,99999).'.png';
$target_path = FCPATH.('uploads/post-image/'.$target_file_name);
$target_path_1 = base_url().'uploads/post-image/'.$target_file_name;
exec("convert ".$process_file_path." -gravity center -fill white -font Corsiva -pointsize 30 -interline-spacing 40 -annotate +0+0 '".$smart_wordwrap_info."' ".$target_path);
$upload_image[] = $target_path_1;
#unlink($process_file_path);
}
I tried something like this, but it's not working.
exec("convert ".$process_file_path." -gravity center -fill white -font Corsiva -pointsize 30 -interline-spacing 40 -annotate +0+0 '".($smart_wordwrap_info |sed "s/'/\'/g)."' ".$target_path);

Imagick center (horizontal, vertical)

I want to center text over an image, horizontal and vertical. And let it automatically fit (unless what the content is).
When I use the command line, this command works almost perfectly (needs some little finetuning):
magick image.jpg -gravity Center -size 320x140 caption:'This text is resized to best fill the space given.' -compose over -composite ouput.jpg
However, now I want to achieve the same with PHP, but the text does not resize over multiple lines, so I am doing something wrong.
The script I've created so far:
$text = 'This text is resized to best fill the space given.';
$width = 600;
$height = 600;
$textBackground = new ImagickPixel('transparent');
$textColor = new ImagickPixel('#000');
$gradient = new Imagick('image.jpg');
$image = new Imagick();
$image->newImage($width, $height, $textBackground);
$gradient->setImageColorspace($image->getImageColorspace());
$draw = new ImagickDraw();
//$draw->setFillColor($textColor);
$draw->setFontSize( 36 );
$draw->setFont("Alegreya.ttf");
$draw->setGravity (Imagick::GRAVITY_CENTER);
$image->annotateImage($draw, 0, 0, 0, $text);
$gradient->compositeImage($image, Imagick::COMPOSITE_OVER, 0, 0);
$gradient->setImageFormat('jpg');
header('Content-type: image/jpg');
echo $gradient;
The output this generates:

How to resize an animated gif with ImageMagick (php)?

I cannot find a working example. This one is not working, it's not animated:
$imagick = new Imagick($_FILES['file']['tmp_name']);
$format = $imagick->getImageFormat();
if ($format == 'GIF') {
$imagick = $imagick->coalesceImages();
do {
$imagick->resizeImage(500, 500, Imagick::FILTER_BOX, 1);
} while ($imagick->nextImage());
$imagick = $imagick->deconstructImages();
$imagick->writeImages($name, true);
}
$imagick->clear();
$imagick->destroy();
In ImageMagick command line, this works for me
Original Animation:
convert animation.gif -coalesce -resize 500x500 -layers optimize -loop 0 resize.gif
Did you do the equivalent of -coalesce after reading your input animation? Did you do the optimize step after resizing?
Sorry, I do not use PHP Imagick or any other API.

How to add on my image gif's watermark with php?

I try this:
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$image = imagecreatefromstring(file_get_contents($path));
$w = imagesx($image);
$h = imagesy($image);
$centerX=round($w/4);
$centerY=round($h/2);
$watermark = imagecreatefrompng('lool-face.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);
imagecopy($image, $watermark, $centerX, $centerY, 0, 0, $ww, $wh);
eregi('\.(gif|jpeg|jpg|png)$',$path,$regs);
switch( $regs[1] ) {
case 'gif':
header('Content-type: image/gif');
imagegif($image);
break;
but this don't working, the images is curdle.
And try this:
shell_exec('for i in sphere*.gif; do convert $i -font Arial -pointsize 20 \
-draw "gravity south \
fill black text 0,12 \'Copyright\' \
fill white text 1,11 \'Copyright\' " \
wmark_$i; done');
shell_exec("convert -delay 20 -loop 0 wmark_sphere*.gif animatespheres.gif");
$f = fopen("animatespheres.gif", 'rb');
fpassthru($f);
fclose($f);
This code does not work I do not know why it does not.
We wish you a directory where there Gif htaccess can do it in gifs watermarking
This class could be your solution:
PHP Classes transparent watermark
Take a look at the link below. I have done the same thing in php. I hope that this will helps you.
https://dl.dropboxusercontent.com/u/58146160/watermark%20image.rar

Using image to set textured background using imagettftext

I can create an image using GD with PHP using the imagettftext() function. The colour of the text is specified using imagecolorallocate() but this takes colour in the format RGB.
I have a range of textured images - each 10 x 10 pixels. I would like to use these textured images as the colour for the text rather than a single RBG colour.
I can't see how to achieve this, is it possible?
If you can, use ImageMagick. It can do this out of the box.
Example from the examples:
convert -size 800x120 xc:black -font Corsiva -pointsize 100 \
-tile tile_disks.jpg -annotate +20+80 'Psychedelic!' \
-trim +repage -bordercolor black -border 10 funfont_groovy.jpg
I made this function which replaces pixel by pixel based on a defined color
function imagettftexture(&$im,$textureimage, $size, $angle, $_x, $_y, $font, $text){
$w = imagesx($im);
$h = imagesy($im);
$sz = getimagesize($textureimage);
$tx = null;
switch($sz['mime']){
case 'image/png':
$tx = imagecreatefrompng($textureimage);
break;
case 'image/jpeg':
$tx = imagecreatefromjpeg($textureimage);
break;
case 'image/gif':
$tx = imagecreatefromgif($textureimage);
break;
}
//same size as $im
$tmp = imagecreatetruecolor($w,$h);
//fill with texture
imagesettile($tmp,$tx);
imagefilledrectangle($tmp, 0, 0, $w, $h, IMG_COLOR_TILED);
//a weird color
$pink = imagecolorclosest($im,255,0,255);
$rect = imagettftext($im,$size,$angle,$_x,$_y,-$pink,$font,$text); // "minus" to draw without aliasing
for($x=0;$x<$w;$x++){
$x = $x;
for($y=0;$y<$h;$y++){
$tmpcolor = imagecolorat($tmp,$x,$y);
$color = imagecolorat($im,$x,$y);
if($color == $pink)
imagesetpixel($im,$x,$y,$tmpcolor);
}
}
//useful to return same value as imagettftext
return $rect;
}
It's pretty slow, it took
100ms to process a 350x127 image
760ms to process a 1024x342 image
There are other solutions here

Categories