How to add on my image gif's watermark with php? - 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

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:

Append one or more gif animations through php

How to add one or more gif animations.
// Create image instances
$dest = imagecreatefromgif('php.gif');
$src = imagecreatefromgif('php.gif');
// Copy and merge
imagecopymerge($dest, $src, 10, 10, 0, 0, 100, 47, 75);
// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
The code above is not working.
http://www.imagemagick.org/Usage/anim_mods/
$str = "Mary Had A Little Lamb and She LOVED It So";
echo $str = str_replace(' ','',(strtolower($str)));
exec('convert alphabets/1/a.gif -repage 92x62 -coalesce null: ( alphabets/1/b.gif -coalesce ) -geometry +46+0 -layers Composite out.gif');
list($width, $height) = getimagesize('alphabets/1/e.gif');
list($out_width, $out_height) = getimagesize('out.gif');
$width1 = $out_width+$width;
exec('convert out.gif -repage '.$width1.'x'.$height.' -coalesce null: ( alphabets/1/e.gif -coalesce ) -geometry +'.$out_width.'+0 -layers Composite out1.gif');

PHP GD - Watermark image with opacity

I am trying to add a watermark to an image using PHP and the GD image library. I can apply the watermark where I specify with the correct opacity setting.
The problem is that my watermark itself has a transparent background. When I try to apply this watermark to the image I get a black background.
The image which the watermark is being applied to is a jpeg. Could this be the problem? If so how would I convert the jpeg into a format which supports transparency, apply watermark, then convert it back?
This is the key bit of code I have at the moment.
// Determine image size and type
$size = getimagesize($this->image_path);
$size_x = $size[0];
$size_y = $size[1];
$image_type = $size[2]; // This is always a JPEG
// load source image
$image = $this->ImageCreateFromType($image_type, $this->image_path);
// Determine watermark size and type
$wsize = getimagesize($watermark_path);
$watermark_x = $wsize[0];
$watermark_y = $wsize[1];
$watermark_type = $wsize[2]; // This is typically a PNG
// load watermark
$watermark = $this->ImageCreateFromType($watermark_type, $watermark_path);
$dest_x = $this->setX($size_x, $watermark_x);
$dest_y = $this->setY($size_y, $watermark_y);
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_x, $watermark_y, $this->opacity);
While not really relevant, here is the code for the ImageCreateFromType function
function ImageCreateFromType($type,$filename) {
$im = null;
switch ($type) {
case 1:
$im = ImageCreateFromGif($filename);
break;
case 2:
$im = ImageCreateFromJpeg($filename);
break;
case 3:
$im = ImageCreateFromPNG($filename);
imagealphablending($im, true);
imagesavealpha($im, true);
break;
}
return $im;
}
Have a read about the imagecolortransparent() function: http://php.net/manual/en/function.imagecolortransparent.php
You may also want to look at this question: Can PNG image transparency be preserved when using PHP's GDlib imagecopyresampled?

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