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
Related
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:
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
I am experiencing an issue where I am allowing the user to resize images in a container and then need to create a resulting image that is the size of the container but with the image scaled and adjusted as per the users choices.
So for example say the container is 400 x 200 and the user wants to be able to put in a logo that is 600 x 100 they may wish to shrink the logo so it fits and leave space at the top and bottom. I need to be able to save that as an image that is 400x200 with the appropriate gaps at the top and bottom.
What I have found though is that if the image content (the logo in this example) extends beyond BOTH the top and the right of the container everything is fine OR if it DOESNT extend beyond either it is fine but if it extends beyond one and not the other then I get black fill- or something like that- see examples below...
Below are some examples of the results and this is the code I am using...
$cropped = wp_imagecreatetruecolor( $frame_w, $frame_h);
$backgroundColor = imagecolorallocatealpha($cropped, 0, 0, 0, 127);
//imageantialias( $cropped, true );
//if $img_y or $img_x are negative we need to apply the value to $img_x and $img_y
//if $img_y or $img_x are positive we need to apply the value to $dest_x and $dest_y
$dest_x = strstr($img_x,'-') ? 0 : abs($img_x);//if neg is true = 0 else offset inside
$dest_y = strstr($img_y,'-') ? 0 : abs($img_y);
$img_x = strstr($img_x,'-') ? abs($img_x) : 0;//if neg is true offset outside else 0
$img_y = strstr($img_y,'-') ? abs($img_y) : 0;
$img_w = $img_w > $frame_w ? $frame_w : $img_w;
$img_h = $img_h > $frame_h ? $frame_h : $img_h;
imagecopy( $cropped, $resized, $dest_x, $dest_y, $img_x, $img_y, $img_w, $img_h);
//imagecopymerge( $cropped, $resized, $dest_x, $dest_y, $img_x, $img_y, $img_w, $img_h,100);
//imagecopyresampled( $cropped, $resized, $dest_x, $dest_y, $img_x, $img_y, $frame_w, $frame_h, $img_w, $img_h );
imagefill($cropped, 0, 0, $backgroundColor);//putting this after the copy makes any black borders transparent again unless $resized does not extend beyond both dimensions
Examples
Image does not extend beyond top or beyond right (fine)
Image extends beyond bottom but not right (not fine)
Image extends beyond both (fine)
Image extends beyond right but not bottom (not fine)
Image Extends beyond neither (fine)
I have been literally tearing my hair out trying to fix this and tried every possible combination of imagesavealpha, imagecopymerged, imagecolorallocatealpha, imagealphablending etc I can think of but nothing seems to fix this...
So is this a bug/limitation of GD? Or can someone out there come to my rescue!
I don't know if this will help you, but I had an issue with this earlier today. The box expands but the area was black. Here's my code (which fixes it):
<?php
function createImage($text)
{
// Adds an extra space to fill underline
$text = " $text";
// Adds one line at the end
$text .= "\n";
// Wrap the text to fit the image
$text = wordwrap($text, 40, "\n");
// Count new lines
$newlines = substr_count($text, "\n");
// Count how long to expand
if($newlines == 0)
{
$height = 30;
}
else
{
$height = 30*$newlines-$newlines*5;
}
putenv('GDFONTPATH=' . realpath('.'));
header('Content-Type: image/png');
// Adding underline
$e = explode('<', $text);
for($i=0;$i<count($e);$i++)
{
$e[$i] = implode('̲', str_split($e[$i]));
}
// Creating image
$text = implode(' ', $e);
$im = imagecreatetruecolor(315, $height);
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$purple = imagecolorallocate($im, 97, 26, 139);
imagefilledrectangle($im, 0, 0, 399, $height, $white);
$font = 'arialbd.ttf';
imagettftext($im, 11, 0, 10, 20, $purple, $font, $text);
imagepng($im);
imagedestroy($im);
}
createImage("asbdasddsa");
?>
i think this will happen only for PNG check with other formats
I'm not sure if this is an actual answer since it is basically 'Use ImageMagick' but anyway for those for whom ImageMagick is an option the code below might help them achieve the same thing as I was trying to above... Basically ImageMagick seems far superior to GD, no borders around rotated images, no hassles with transparency, no onwanted black fill, clearer resizing if you are enlarging...
$img_x = -50; //left offset of the image within the frame
$img_y = 50; //top offset of the image within the frame
$img_w = 400; //width of the image to be put in the frame
$img_h = 200; // height of the image to be put in the frame
$angle = 45; //rotation to be applied to the image before it is put into the frame
$frame_w = 300; //width of the frame the image is going into
$frame_h = 300; //height of the frame the image is going into
$img_path = 'path/to/image/file.jpg';
$image = new Imagick( $img_path );
$size = $image->getImageGeometry();
$orig_w = $size['width']; $orig_h = $size['height'];
$image->scaleImage( $img_w, $img_h );
//rotate if necessary
if($angle)
{
$image->rotateImage( new ImagickPixel('none'), $angle );
$size = $image->getImageGeometry();
$img_w = $size['width']; $img_h = $size['height'];
}
//composite into frame
//in imagemagick we create an image that is the size of the frame and make it transparent
$frame = new Imagick();
$frame->newImage($frame_w, $frame_h, new ImagickPixel("none"));
//then we composite the image itself into this with the respective offset values
$frame->compositeImage( $image, Imagick::COMPOSITE_DEFAULT, $img_x, $img_y );
//save it
$destfilename = "{$dir}/{$name}-{$img_suffix}.{$ext}";
$frame->writeImage($destfilename);
$frame->clear();
$frame->destroy();
$image->clear();
$image->destroy();
The above code produces this...yay!
I use this function to resize images but i end up with ugly creepy image with a black background if it's a transparent GIF or PNG with alpha, however it works perfectly for jpg and normal png.
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
switch ($stype)
{
case "png":
imagealphablending( $dimg, false );
imagesavealpha( $dimg, true );
$transparent = imagecolorallocatealpha($dimg, 255, 255, 255, 127);
imagefilledrectangle($dimg, 0, 0, $nw, $nh, $transparent);
break;
case "gif":
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($simg, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($simg, $background);
break;
}
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w> $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w <$h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
Example : cropImage("300","200","original.png","png","new.png");
I use php 5.3.2 and the GD library bundled (2.0.34 compatible)
How to make it support transparency? i've added imagealphablending() and imagesavealpha but it didn't work. Or atlast is there any similar good classes?
Thanks
The ugly black background disappears if you output the image to png. So here are the two alternative solutions, both tested:
If you can store the thumbnail as png, just do it: change imagejpeg($dimg,$dest,100); to imagepng($dimg,$dest);
If it's important to store it as jpeg, remove the lines imagealphablending( $dimg, false ); and imagesavealpha( $dimg, true ); -- the default values of true and false, respectively, will provide the desired effect. Disabling alpha blending only makes sense if the result image also has an alpha channel.
I haven't tested it myself but this was an idea I've had for doing exactly that when working on a project of mine.
First, find a color that isn't used in the image and create a new image with that as the background (really flashy green for example, just like they do with motion capture)
Next, copy your image with transparency over it (I know this works with PHP I used to do this to put borders over images)
Then, use the function imagecolortransparent to define which color in that image is transparent and give it your flashy green color.
I think it would work but I haven't tested it.
I have a collection of black and white JPEG's stored on my server. These images are symbol based, where the symbol is a collection of black lines on a white background.
I am trying to use GD to replace the black colour with another colour on the fly based on a variable passed. Currently, I am:
Getting the JPEG as: $image = imagecreatefromjpeg($imgURL),
Converting a HEX code (#FF0000, say) to RGB through PHP,
And then feeding these variables to:
private function colourize_image($image, $colour, $contrast = 0) {
if (!$image) { return false; }
imagealphablending($image, true);
imagesavealpha($image, true);
# Convert hex colour into RGB values
$r = hexdec('0x' . $colour{0} . $colour{1});
$g = hexdec('0x' . $colour{2} . $colour{3});
$b = hexdec('0x' . $colour{4} . $colour{5});
imagefilter($image, IMG_FILTER_COLORIZE, $r, $g, $b);
imagefilter($image, IMG_FILTER_CONTRAST, $contrast);
# Return the GD image object
return $image;
}
For some reason, the function doesn't work at all (it won't overlay a new colour).
Can anyone advise as to where I am going wrong?
Many thanks.
If the color is the only problem, then you could try this:
<php>
//SNIP
$color = preg_replace('/^#/','',$color); //Get rid of "#" if it's there
$r = hexdec("0x{$color[0]}{$color[1]}");
$g = hexdec("0x{$color[2]}{$color[3]}");
$b = hexdec("0x{$color[4]}{$color[5]}");
//SNIP
</php>
You can use the imageistruecolor function to find out whether the JPEG you've just loaded is true colour or palette-based. If it's not true colour, you can create a new true color image of the same width and height, and copy the old image over:
$width = imagesx($jpeg);
$height = imagesy($jpeg);
$image = imagecreatetruecolor($width, $height);
imagecopy($jpeg, $image, 0, 0, 0, 0, $width, $height);
You should then be able to apply the new colours.