I know it is an old question but I am not able to solve it. Here is my code
function add_ZK_mark($inputfile, $outputfile) {
$im = #imagecreatefromjpeg($inputfile);
$bg = #imagecolorallocate($im, 255, 255, 255);
$textcolor = #imagecolorallocate($im, 0, 0, 255);
list($x, $y, $type) = getimagesize($inputfile);
#imagejpeg($im, $outputfile);
#imagedestroy($im);
}
I am getting jpeg image type everytime. Using signature.js to get image in jpeg format.
Thanks
Related
The code suppose show the image like this below
however the result is this below
this is my code here
simplegraph.php
<?php
// set up image canvas
$height = 200;
$width = 200;
$im = imagecreatetruecolor(width, height)($width, $height);
$white = imagecolorallocate ($im, 255, 255, 255);
$blue = imagecolorallocate ($im, 0, 0, 255);
// draw on image
imagefill($im, 0, 0, $blue);
imageline($im, 0, 0, $width, $height, $white);
imagestring($im, 4, 50, 150, 'Sales', $white);
// output image
header('Content-type: image/png');
imagepng ($im);
// clean up
imagedestroy($im);
?>
here is all files in
htdocs
This is probably because of this statement:
$im = imagecreatetruecolor(width, height)($width, $height);
The above is incorrect syntax. Change this to:
$im = imagecreatetruecolor($width, $height);
The image will show after this, because PHP is assuming, that width and height are constants, but they do not exist and it will give out error, and after that the ($width, $height) part will cause the syntax error. But that is the part that you actually need.
Can someone help me to draw single pixel using php GD library?
I have such code:
$img = imagecreatetruecolor(100, 100);
$c = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
imagesetpixel($img, 0, 0, $c);
imagejpeg($img, 'created.jpg');die();
But the output is really weird (it is approximate image in top-left corner):
Is it possible to draw just one red pixel at x-0 y-0 position?
Basically when I draw text it's ending up black like this: http://i.stack.imgur.com/z675F.png
Instead of the color I'm allocated in PHP and the function.
Code:
$finalImage = imagecreatefrompng($imageFile);
$logo = imagecreatefrompng($logoImage);
imagecopy($finalImage, $logo, $logoPosition['x'], $logoPosition['y'], 0, 0, imagesx($logo), imagesy($logo));
$font = "arial.ttf";
$fontSize = 10;
$yOffSet = 15;
$white = imagecolorallocate($finalImage, 255, 255, 255);
foreach($pixelArray as $key => $x) {
foreach($valueArray[$key] as $valueText) {
imagettftext($finalImage, $fontSize, 0, $x, $yOffSet, $white, $font, $valueText);
$yOffSet += 15;
}
$yOffSet = 15;
}
if($miscText != null) {
foreach($miscText as $key => $text) {
imagettftext($finalImage, $fontSize, 0, $text['x'], $text['y'], $white, $font, $text['text']);
}
}
imagepng($finalImage,$saveFileName.".png");
imagedestroy($finalImage);
It was working before, but then it just stopped and I have no clue why. It was after I changed the source image (Was generating fine) and I hadn't touched the code. I've tried all sorts of things with changing the colors, but I can't get it to display in anything other then black.
Fixed it by changing imagecolorallocate to imagecolorclosest since I already some white text on a logo I copy in:
// imagecolorallocate....
$white = imagecolorclosest($im, 255, 255, 255);
Have you checked if the imagecolorallocate() function is returning boolean false, as it will if the allocation fails? If the $finalImage .png is 8bit, and your pure white color isn't in the source image's palette, this call will fail. You do say you changed the source image, so this is most likely why it's broken now.
$white = imagecolorallocate($finalImage, 255, 255, 255);
if ($white === FALSE) { // note the === -> strict type comparison
die("Failed to allocate color 255/255/255")
}
The function will also simply return a number representing the color triplets, in this case it would 0xFFFFFF. You can try passing that into the imagegetttftext() call directly, see if that helps.
Use imagecreatetruecolor() to allocate the color:
$width = 500; //whatever width you need
$height = 200; //whatever height you need
$white = imagecolorallocate(imagecreatetruecolor($width, $height), 255, 255, 255);
That worked for me.
What is the best way to replace transparent colors with white in gif and png images with php?
// get transparent color indexes
$trsp = ImageColorsForIndex($image, ImageColorTransparent($image));
// get transparent color set
$delete = imagecolorallocate($image, $trsp['red'], $trsp['green'], $trsp['blue']);
// replace
imagecolorset($image, $delete, 255, 255, 255);
does not working.
I don't really use GD all that much - I prefer ImageMagick. The following method works, but I'm not sure if it is the most efficient:
// Get the original image.
$src = imagecreatefrompng('trans.png');
// Get the width and height.
$width = imagesx($src);
$height = imagesy($src);
// Create a white background, the same size as the original.
$bg = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($bg, 255, 255, 255);
imagefill($bg, 0, 0, $white);
// Merge the two images.
imagecopyresampled(
$bg, $src,
0, 0, 0, 0,
$width, $height,
$width, $height);
// Save the finished image.
imagepng($bg, 'merged.png', 0);
I tried something like this but it just makes the background of the image white, not necessarily the alpha of the image. I wanted to just upload everything as jpg's so if i could somehow "flatten" a png image with some transparently to default it to just be white so i can use it as a jpg instead. Appreciate any help. Thanks.
$old = imagecreatefrompng($upload);
$background = imagecolorallocate($old,255,255,255);
imagefill($old, 0, 0, $background);
imagealphablending($old, false);
imagesavealpha($old, true);
<?php
$input_file = "test.png";
$output_file = "test.jpg";
$input = imagecreatefrompng($input_file);
$width = imagesx($input);
$height = imagesy($input);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $input, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file);