I have a 1x2px image, when I use imagerotate I get this error
Warning: imagerotate(): gd warning:
one parameter to a memory allocation multiplication is negative or zero,
failing operation gracefully in file#line
Sample code
header('Content-type: image/png');
$angle = 320;
$image = imagecreatefrompng('small.png');
$bg_color = imagecolorallocatealpha($image, 0, 0, 0, 127);
$temp_image = imagerotate($image, -$angle, $bg_color);
imagesavealpha($image, 1);
imagepng($image);
Is there a workaround for this issue?
PHP 5.5.12
WAMP
Related
So I found some code on PHP Doc, and edited it slightly to merge two images I have. The image is then saved in a folder on the server. However there is a slight problem and I am unable to figure out why it is happening.
Firstly my code:
$glassurl = $_GET['GlassImg'];
$frameurl = $_GET['FrameImg'];
$filename = (int)date("H:i:s");
$src = imagecreatefromgif($frameurl);
$dest = imagecreatefromjpeg($glassurl);
imagecolortransparent($src, imagecolorat($src, 0, 0));
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagealphablending($src, false);
imagesavealpha($src, true);
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
imagepng($dest, 'uploads/imagetest.png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src
);
Secondly some information about the images:
Both Images are exactly the same size
The 'pattern' image is just a block colour/pattern
The frame image has transparent parts within the frame (to allow pattern to show through)
The area around the frame is white to hise the excess pattern
I was hoping that when I overlayed the frame onto the pattern because of these parts that it would produce a window frame, with the glass pattern inside, and the white would hide the remaining patten.
To illustrate I have provided the images. and what happens.
Pattern:
Frame:
Result:
As you can see it doesn't result in what I expected. Can anyone please tell me where I am going wrong? I want to overlay the frame onto the pattern, keeping the transparent center and using the excess white to cover the rest of the patter. Any help is greatly appreciated.
Please note that your frame has white edges and if you sill want the windows to be wite you need to crop it and remove the imagecolortransparent added below if not you can use this
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
imagecolortransparent($src, imagecolorat($src, 0, 0));
$src_x = imagesx($src);
$src_y = imagesy($src);
imagecopymerge($dest, $src, 0, 0, 0, 0, $src_x, $src_y, 100);
// Output and free from memory
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output
You can also have
$imgl = "thumb/pattern.png";
$img2 = "thumb/frame.png";
$dest = imagecreatefrompng($imgl);
$src = imagecreatefrompng($img2);
$src_x = imagesx($src);
$src_y = imagesy($src);
$srcNew = imagecreatetruecolor($src_x, $src_y);
ImageColorTransparent($srcNew, imageColorAllocate($srcNew, 0, 0, 0));
imagecopy($srcNew, $src, 70, 50, 78, 60, 473, 293);
imagecopymerge($dest, $srcNew, 0, 0, 0, 0, $src_x, $src_y, 100);
header('Content-Type: image/png');
imagegif($dest);
imagedestroy($dest);
imagedestroy($src);
Output
Your image is not transparent as you described, try using this instead if I understood what you described correctly.
also you should find a program which does not transform transparency to white when saving (or check for options regarding this) if you really made those transparent in the first place.
I have a problem with the imagecolorallocatealpha in PHP. When setting the opacity to 127, I get a white image instead of a transparent one.
Here is my code
$image = imagecreatetruecolor($width, $height);
imagesavealpha($image, true);
$color = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefill($image, 0, 0, $color);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit;
I have also tried this one but I get the same result
$image = imagecreatetruecolor($width, $height);
$x = imagecolorat($image, 0,0);
imagecolortransparent($image, $x);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
exit;
Any idea ? Can it be related to server configuration ?
Replace the first line
$image = imagecreatetruecolor($width, $height);
with
$image = imagecreate($width, $height);
Transparancy should now work, but the colour palet won't show some true colours corectely.
Any white pixels will now be transparant.
To change the transparancy colour from white, use:
$r = *red colour value (0 to 255)*;
$g = *green colour value (0 to 255)*;
$b = *blue colour value (0 to 255)*;
$color = imagecolorallocatealpha($image, $r, $g, $b, 127);
If that's all the code you are using for the image file, you haven't defined any values for the $width and $height variables, and the script throws an error.
As a general rule, when you want to debug an image, remove the Content-Type header. That way, you can view any potential errors by accessing the script in a browser.
I am provided with a hosting plan with php gd version 2 and I cannot install any other library. I know image flipping can be done by using imagesx() imagesy() and imagecreatetruecolor() but they are not available in GD version 2. and I cannot upgrade to higher version.
So, is there any other way to flip image horizontally and vertically using php gd version 2 or just with php? Thanks millions.
Maybe this will help... you will need to modify it to flip vertical though...
$size_x = imagesx($img);
$size_y = imagesy($img);
$temp = imagecreatetruecolor($size_x, $size_y);
imagecolortransparent($temp, imagecolorallocate($temp, 0, 0, 0));
imagealphablending($temp, false);
imagesavealpha($temp, true);
$x = imagecopyresampled($temp, $img, 0, 0, ($size_x-1), 0, $size_x, $size_y, 0-$size_x, $size_y);
if ($x) {
$img = $temp;
}
else {
die("Unable to flip image");
}
header("Content-type: image/gif");
imagegif($img);
imagedestroy($img);
Credit to Markus: Here is the link
Since last 2 days, I was trying to add transperancy to the background after rotating an image using imagerotate() PHP-GD function.
But, to my great disappointment, it's not working at all.
It's just giving out a black background behind it.
Here's my code -
$patchImageS = 'image.png'; // the image to be patched over the final bg
$patchImage = imagecreatefrompng($patchImageS); // resource of image to be patched
$patchImage = imagerotate($patchImage, 23, 0, 0);
imagepng($patchImage,'tt.png');
I tried to change the parameters being passed in function to
imagerotate($patchImage, 23, 5, 0);
imagerotate($patchImage, 23, 0, 5);
Any help would be highly appreciated.
After a number of 99% finished answers, here's the solution I've found:
// Create, or create from image, a PNG canvas
$png = imagecreatetruecolor($width, $height);
// Preserve transparency
imagesavealpha($png , true);
$pngTransparency = imagecolorallocatealpha($png , 0, 0, 0, 127);
imagefill($png , 0, 0, $pngTransparency);
// Rotate the canvas including the required transparent "color"
$png = imagerotate($png, $rotationAmount, $pngTransparency);
// Set your appropriate header
header('Content-Type: image/png');
// Render canvas to the browser
imagepng($png);
// Clean up
imagedestroy($png);
The key here is to include your imagecolorallocatealpha() in your imagerotate() call...
look for imagesavealpha() in the php-documentation - i think this is what you are looking for.
EDIT: here's an example:
$png = imagecreatefrompng('./alphachannel_example.png');
// Do required operations
$png = imagerotate($png, 23, 0, 0);
// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);
// Output image to browser
header('Content-Type: image/png');
imagepng($png);
imagedestroy($png);
For anyone having problems with imagecopyresampled or imagerotate with black bars on background, I have found a code example here:
https://qna.habr.com/q/646622#answer_1417035
// get image sizes (X,Y)
$wx = imagesx($imageW);
$wy = imagesy($imageW);
// create a new image from the sizes on transparent canvas
$new = imagecreatetruecolor($wx, $wy);
$transparent = imagecolorallocatealpha($new, 0, 0, 0, 127);
$rotate = imagerotate($imageW, 280, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
// get the newest image X and Y
$ix = imagesx($rotate);
$iy = imagesy($rotate);
//copy the image to the canvas
imagecopyresampled($destImg, $rotate, 940, 2050, 0, 0, $ix, $iy, $ix, $iy);
Hey having some trouble trying to maintain transparency on a png when i create a thumbnail from it, anyone any experience with this? any help would be great, here's what i am currently doing:
$fileName= "../js/ajaxupload/tees/".$fileName;
list($width, $height) = getimagesize($fileName);
$newwidth = 257;
$newheight = 197;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagesavealpha($thumb, true);
imagepng($thumb,$newFilename);
I have had success doing it like this in the past:
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
$source = imagecreatefrompng($fileName);
imagealphablending($source, true);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($thumb,$newFilename);
I found the output image quality much better using imagecopyresampled() than imagecopyresized()
Forget the color transparency index, it never works in all rendering products. Instead use an alpha layer mask:
$image = imagecreatetruecolor($size, $size);
imagealphablending($image, false);
imagesavealpha($image, true);
$trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
imagefill($image, 0, 0, $trans_layer_overlay);
Those functions access the underlying gdlib library, which is a fine toy, but not something that makes for nice results. If you have the option, use imagemagick instead. The downside is that there are currently no good php-bindings, so you need to access it over the shell, which you're usually not allowed on shared hosts.
See dycey's answer to "How do I resize...". Essentially, you need to fill the entire background with transparency before you do any other operations.
imagecopyresized does not support transparency properly.
imagecopymerge does, but it doesn't resize.
The solution? You'd probably end up resizing the thing manually.