php GD draw a straight line in angle - php

I made that picture in PHP GD, But its have a small problem with lines.
The lines you see is created by imageline().
So when I create a line with an angle using it, the lines had some noises.
Sorry for my bad English.

You could possibly try with imageantialias(). Something like
imageantialias($image, true);
which will result a line a little bit better looking than yours.

Related

php imagerotate() ruins alpha on png?

I've been bashing my head agains something simple..
// ....all prev code is fine....
$pasteboard =imagecreatetruecolor($imgs['bg']["width"],$imgs['bg']["height"]);
imagealphablending($pasteboard, false);
imagecopyresampled($pasteboard, $imgs['bg']["img"],0,0,0,0,$imgs['bg']["width"],$imgs['bg']["width"],imagesx($imgs['bg']["img"]),imagesy($imgs['bg']["img"]));
imagecopyresampled($pasteboard, $imgs['photo']["img"],20,20,0,0,$imgs['photo']["width"],$imgs['photo']["width"],imagesx($imgs['photo']["img"]),imagesy($imgs['photo']["img"]));
imagesavealpha($pasteboard,true);
//send it out
$out = $pasteboard;
header('Content-type: image/png');
imagepng($out);
//then garbage collection
gives me this:
HORAY!
perfect alpha png composite...
Now I want to rotate it, so instead of the $out=$pasteboard i do this:
imagesavealpha($pasteboard,true);
//rotate it
$out = imagerotate($pasteboard,5,imagecolorexactalpha($pasteboard,255,255,255,50),0);
header('Content-type: image/png');
imagepng($out);
which sadly gives me this:
BOOOO!
Ive tried setting the color like:
imagerotate($pasteboard,5,0x00000000,0);
also the last attr like:
imagerotate($pasteboard,5,0x00000000,1);
new empty images sampled etc etc...
no dice....
Can anyone help?
I'm answering my question simply because I've tried 10-15 suggestions i've seen allover the web all of which offering 'nearly' right solutions but nothing exact, Also I've seen this question posted a few places now, and hopefully if anyone reaches this page in future it would be best to show the solution as the direct answer.
MASSIVE thanks to #cristobal for the help and efforts, if I could vote you up any more I would !
The knack seems to be:
//rotate it
$pasteboard = imagerotate($pasteboard,5,0XFFFFFF00,0); //<-- here must be RRGGBBAA, also last attr set to 0
imagesavealpha($pasteboard, true); // <-- then to save it... dont ask me why..
//send it out
header('Content-type: image/png');
imagepng($pasteboard);
produces this (it has a perfect alpha even though you cant see against the white page):
REALLY not the most fun 5 hrs of my life... hopefully it will stop someone else going through the same pain..
Using the same code above and using a blue color for the third parameter in the imagerotate operation, which will be it used to fill the uncovered zone after the rotation i.e.:
imagerotate($pasteboard, 5, 255);
We get the following image
we see the blue area is the uncovered zone which it fills, while the black color is the to be the border shadow from the image which GD does not seem to handle well along the interpolation used in the rotation.
The same image rotated using the convert for imagemagick. commmand i.e. $> convert -rotate 5 image.png image_rotated.png results in the image below
Clearly GD does not handle alpha colors well when rotating.
If you have access to use the convert commmand using exec or process, you should pipe those image operation to imagemagick instead. GD is a simple image library which has not been updated much the latest years. Otherwise try Imagemagick, Cairo or Gmagick which there are pecl plugins for too http://php.net/manual/en/book.image.php.
Last resort somebody made a function that which uses GD http://www.exorithm.com/algorithm/view/rotate_image_alpha for what you are looking after but the result is not pretty since its a simple linear interpolation:
taken from How to rotate an image in GD Image Library while keeping transparency?. Perhaps if you convert the linear interpolation function to a Bicubic or Quad it will look better.
Note these answers did not work for me but this did.
$destimg = imagecreatefromjpeg("image.png");
$rotatedImage = imagerotate($destimg, 200, 0);
imagesavealpha($rotatedImage, true);
imagepng($rotatedImage,"rotated.png");

Merging RGB channels back into a single image with ImageMagick (php)

So I have a situtation with ImageMagick and php where I need to processes each of the RGB channels separately and then merge them back together into the final image. So in the code I have below, $red, $green and $blue are the channels (as gray scale images). The following code is what I have tried (and a couple variations) but each time I end up with an image that only has Cyan, Magenta or Yellow showing through. In this case, the resulting image is Cyan.
$im->removeImage();
$im->addImage($red);
$im->addImage($green);
$im->addImage($blue);
$img = $im->combineImages(self::CHANNEL_ALL);
$im->removeImage();
$im->removeImage();
$im->removeImage();
$im->addImage($img);
I think part of my problem is that the PHP documentation doesn't say much about how to use combineImages and there are no samples so far as I can find. So it's very likely that I'm using that particular method incorrectly, and I suspect it has to do with how I am combining the images in the single Imagick object to begin with.
EDIT
This question ultimately boils down to this: How do I recreate the following script using only php?
convert tmp_r.png tmp_g.png tmp_b.png -combine tmp_rgb.png
[EDIT]
I have to admit, looking further into the documentation, Im not sure what the constant CHANNEL_ALL does. They do state that you can concatenate channels by logically ORing them together. You might try:
$im->combineImages(imagick::CHANNEL_RED | imagick::CHANNEL_GREEN | imagick::CHANNEL_BLUE);
[ORIGINAL]
I've been looking into this API, and honestly what I think you are looking for is the convert function, NOT the combine function.
Look At the below provided link and click specifically on "Combining RGB Channel Images"
http://www.imagemagick.org/Usage/color_basics/
Try that, leave a comment if you need further help :-)
So I think I've figured out how to get this to work. The missing piece was a call to flattenImages(). I'm not exactly sure why this worked, but it seems to be what I was looking for. Here's the code (keep in mind that $this is in the context of a member method of a class that extends Imagick):
$this->removeImage(); // gets rid of the old, unprocessed image
$rgb = clone $this;
$rgb->addImage($red);
$rgb->addImage($green);
$rgb->addImage($blue);
$rgb->flattenImages(); // this is what was missing above
$rgb = $rgb->combineImages(self::CHANNEL_ALL);
$this->addImage($rgb);
Can anyone comment on why this might be? I expected flattenImages() to merge the three images into one and destroy some of the information, but it appears that it actually tells ImageMagick to process all of the contained images together whereas it was processing them independently previously.
Try this:
$im->addImage($red);
$im->addImage($green);
$im->addImage($blue);
$im->combineImages(imagick::CHANNEL_RED | imagick::CHANNEL_GREEN | imagick::CHANNEL_BLUE);
btw combineImages doesn't return imagick object, but true/false indicating success or failure, so $im will contain your combined image.
Edit:
Apparently combineImages sucks big time, so here's an alternative: imagick::compositeImage
$im->compositeImage($red, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_RED);
$im->compositeImage($green, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_GREEN);
$im->compositeImage($blue, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_BLUE);

Auto New Line In GD Library

I'm using the GD Library to create images from data I'm pulling from an API.
The strings that are returned can sometimes be kind of lengthy, and I'm hoping to find a way to automatically create a new line for text if the string goes too far.
Is there something like this built into the GD library, or will I have to write some code to count the characters and move everything to a new line if it goes too long?
GD is strictly for drawing. You'll need a text layout engine such as Pango.
I am not familier with a built-in function that automatically creates new lines,
so I guess you need to write a php function that sorts the string to "sub-strings"
according to your width length and then use them in your image.
Consider looking at this post:
http://www.php.net/manual/en/function.imagestring.php#90481

Why might my PHP log file not entirely be text?

I'm trying to debug a plugin-bloated Wordpress installation; so I've added a very simple homebrew logger that records all the callbacks, which are basically listed in a single, ultimately 250+ row multidimensional array in Wordpress (I can't use print_r() because I need to catch them right before they are called).
My logger line is $logger->log("\t" . $callback . "\n");
The logger produces a dandy text file in normal situations, but at two points during this particular task it is adding something which causes my log file to no longer be encoded properly. Gedit (I'm on Ubuntu) won't open the file, claiming to not understand the encoding. In vim, the culprit corrupt callback (which I could not find in the debugger, looking at the array) is about in the middle and printed as ^#lambda_546 and at the end of file there's this cute guy ^M. The ^M and ^# are blue in my vim, which has no color theme set for .txt files. I don't know what it means.
I tried adding an is_string($callback) condition, but I get the same results.
Any ideas?
^# is a NUL character (\0) and ^M is a CR (\r). No idea why they're being generated though. You'd have to muck through the source and database to find out. geany should be able to open the file easily enough though.
Seems these cute guys are a result of your callback formatting for windows.
Mystery over. One of the callbacks was an anonymous function. Investigating the PHP create_function documentation, I saw that a commenter had noted that the created function has a name like so: chr(0) . lambda_n. Thanks PHP.
As for the \r. Well, that is more embarrassing. My logger reused some older code that I previously written which did end lines in \r\n.

Writing curved text on an arc (PHP)

So I Googled and found out how to draw an arc: http://www.php.net/manual/en/function.imagearc.php
The only problem is how do I write text $margin pixels from the arc that is curved.
Centre aligned # $degrees.
Here is an example: http://img194.imageshack.us/img194/3667/scan0001fe.jpg
Let me know if this doesn't make sense.
Here is a simple PHP solution found at http://forums.phpfreaks.com/topic/57542-image-manipulation-skewing-text-text-on-a-curve/
You could try this: http://coding.derkeiler.com/Archive/PHP/alt.php/2004-12/0061.html
Imagick has native support for curved text

Categories