I have an image which can show polylines. I can show the image perfectly but when i apply into website which not using header("Content-type: image/png"); i got problem.
Can i show the image that has been edited with PHP in website not just image.
here's my code
<?php
$img = imagecreatefromjpeg('1967_NL_MAJENEresize.jpg'); //you can change image name
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
imagesetthickness($img, 4);
imagefill($img, 0, 0, $white);
imageline($img, 100, 80, 210, 380, $white); //x1, y1, x2, y2
imageline($img, 200, 80, 210, 380, $white);
imageline($img, 200, 80, 310, 580, $white);
imagefilledrectangle ($img, (100-5), (80-5), 100+10, 80+10, $red);
imagefilledrectangle ($img, (210-5), (380-5), 210+10, 380+10, $red);
imagefilledrectangle ($img, (200-5), (80-5), 200+10, 80+10, $red);
imagefilledrectangle ($img, (310-5), (580-5), 310+10, 580+10, $red);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
You shouldn't include the image generating code in existing content, instead have a dedicated script to create the image using the correct header.
Link to the image from your content by using <img> tag
generate-image.php:
<?php
$imagefile = preg_replace('/[^A-Za-z0-9_\-]/', '_', $_GET['filename']);
$img = imagecreatefromjpeg($imgfile); //you can change image name
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
imagesetthickness($img, 4);
imagefill($img, 0, 0, $white);
imageline($img, 100, 80, 210, 380, $white); //x1, y1, x2, y2
imageline($img, 200, 80, 210, 380, $white);
imageline($img, 200, 80, 310, 580, $white);
imagefilledrectangle ($img, (100-5), (80-5), 100+10, 80+10, $red);
imagefilledrectangle ($img, (210-5), (380-5), 210+10, 380+10, $red);
imagefilledrectangle ($img, (200-5), (80-5), 200+10, 80+10, $red);
imagefilledrectangle ($img, (310-5), (580-5), 310+10, 580+10, $red);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
index.php:
Blah Blah
<img src='generate-image.php?filename=<?= $db->row['thumbnail']; ?>'>;
blah blah
Related
I'm trying to create a PNG with some text and a scaled picture in it. Here is the code for just the text, it works fine:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "assets/fonts/arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
With the code above you get the following picture, which is correct:
Now I'm trying to have a small picture in it, So I'm loading the picture from a JPEG file (adidas.jpg). Here's the code
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
imagecolorallocate($label, 0, 0, 0);
// up text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 150, $color, "arial.ttf", "UP UP UP");
// image
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white = imagecolorallocate($pic, 255, 255, 255);
imagefill($label,0,0,$white);
imagedestroy($pic);
// down text
$color = imagecolorallocate($label, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $color, "arial.ttf", "DOWN DOWN DOWN");
header('Content-type: image/png');
imagepng($label);
imagedestroy($label);
die();
?>
And this is what I get:
To my surprise the "down" text disappeared. Why is that? The text added before the picture is fine, the text added after it turns to black for some reason
Your code is bit messy, "DOWN.." text will appear if you remove second:
$color = imagecolorallocate($label, 255, 255, 255);
You dont fill the original image, you try it later but with wrong color ($white is from $pic, not $label).
I cleaned it up:
<?php
session_start();
error_reporting(E_ALL);
$label = imagecreate(500, 500);
$black = imagecolorallocate($label, 0, 0, 0);
$white = imagecolorallocate($label, 255, 255, 255);
imagefill($label, 0, 0, $black);
imagettftext($label, 50, 0, 0, 150, $white, "arial.ttf", "UP UP UP");
$src = imagecreatefromjpeg("adidas.jpg");
$pic = imagecreatetruecolor(500, 500);
imagecopyresampled($label, $src, 0, 0, 0, 0, 150, 150, imagesx($src), imagesy($src));
$white2 = imagecolorallocate($pic, 255, 255, 255);
imagettftext($label, 50, 0, 0, 350, $white, "arial.ttf", "DOWN DOWN DOWN");
ob_end_clean();
header('Content-type: image/png');
imagepng($label);
imagedestroy($src);
imagedestroy($pic);
imagedestroy($label);
die();
?>
I have this script which generates an image:
$im = imagecreatetruecolor(110, 34);
$red = imagecolorallocate($im, 245, 245, 245);
imagefill($im, 0, 0, $red);
$text_color = imagecolorallocate($im, 80, 80, 80);
imagestring($im, 8, 15, 9, image, $text_color);
header('Content-Type: image/jpeg');
imagejpeg($im);
Now I want to save that image. How can I do that? Here is what I have tried so far:
file_put_contents("../img/name.jpg", file_get_contents($im));
What's wrong?
I want to create an png image with transparency using the GD functions of PHP.
Specifically, I'm have text with different opacity levels (for anti-aliasing).
Using the following code I am able to create an alpha in the main part of the background:
//Create image
$image = imagecreatetruecolor($width, $height);
//Set background to opaque
imagecolortransparent($image, imagecolorallocate($image, 0, 0, 0));
Though it does properly create an alpha, in area the image has opacity levels that are not 0% or 100%, it makes black.
How can I correctly create the opacity levels of these areas in the image?
Specifically, I'm have text with different opacity levels (for
anti-aliasing)
Using a different opacity level for text doesn't anti-alias it. And there's no reason to do this since GD outputs anti-aliased text anyway.
Example 1: This creates an image with an opaque black background with white text at various opacity levels
// create image resource.
$img = imagecreatetruecolor(250, 200);
// create image colours.
$black = imagecolorallocate($img, 0, 0, 0);
$white_0 = imagecolorallocatealpha($img, 255, 255, 255, 0);
$white_25 = imagecolorallocatealpha($img, 255, 255, 255, 32);
$white_50 = imagecolorallocatealpha($img, 255, 255, 255, 64);
$white_75 = imagecolorallocatealpha($img, 255, 255, 255, 96);
$white_100 = imagecolorallocatealpha($img, 255, 255, 255, 127);
// set background to opaque black.
imagefill($img, 0, 0, $black);
// output text strings.
imagettftext($img, 20, 0, 10, 30, $white_0, 'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60, $white_25, 'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90, $white_50, 'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $white_75, 'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $white_100, 'arial.ttf', '100% transparent'); // not visible!
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Result 1:
Example 2: If what you want is a transparent background:
// create image resource.
$img = imagecreatetruecolor(250, 200);
// save alpha channel information (for transparent background).
imagealphablending($img, false);
imagesavealpha($img, true);
// create image colours.
$transparent = imagecolorallocatealpha($img, 0, 0, 0, 127);
$black_0 = imagecolorallocatealpha($img, 0, 0, 0, 0);
$black_25 = imagecolorallocatealpha($img, 0, 0, 0, 32);
$black_50 = imagecolorallocatealpha($img, 0, 0, 0, 64);
$black_75 = imagecolorallocatealpha($img, 0, 0, 0, 96);
// set background to transparent.
imagefill($img, 0, 0, $transparent);
// output text strings.
imagettftext($img, 20, 0, 10, 30, $black_0, 'arial.ttf', '0% transparent');
imagettftext($img, 20, 0, 10, 60, $black_25, 'arial.ttf', '25% transparent');
imagettftext($img, 20, 0, 10, 90, $black_50, 'arial.ttf', '50% transparent');
imagettftext($img, 20, 0, 10, 120, $black_75, 'arial.ttf', '75% transparent');
imagettftext($img, 20, 0, 10, 150, $transparent, 'arial.ttf', '100% transparent'); // not visible!
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Result 2:
I have this PHP code:
<?php
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, 5, 15, 5, "$callsign", $black);
ImageString($im, 5, 15, 20, "$name $surname", $black);
ImageString($im, 5, 15, 35, "Location: $location", $black);
ImageString($im, 5, 15, 50, "HUB: $hub", $black);
ImageString($im, 5, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
?>
I want to change the font that PHP uses to write in the image. How can i do that?? I try but I canĀ“t.
Assuming that you mean font:
http://www.php.net/manual/en/function.imageloadfont.php
http://php.net/manual/en/function.imagettftext.php this may help if your using ttf fonts
You can use imageloadfont ();
syntax:
int imageloadfont ( string $file )
Example:
// Create a new image instance
$im = imagecreatetruecolor(50, 20);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Make the background white
imagefilledrectangle($im, 0, 0, 49, 19, $white);
// Load the gd font and write 'Hello'
$font = imageloadfont('./04b.gdf');
imagestring($im, $font, 0, 0, 'Hello', $black);
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
so change your code to:
//Other part of code
Header("Content-type: image/png");
$im = imagecreatefromPng("./images/signatures/background.png");
$font = imageloadfont('./fonts/arial.gdf');//change the parameter based on your font file name
$red = ImageColorAllocate($im, 255, 0, 0);
$black = ImageColorAllocate($im, 0, 0, 0);
ImageString($im, $font, 15, 5, "$callsign", $black);
ImageString($im, $font, 15, 20, "$name $surname", $black);
ImageString($im, $font, 15, 35, "Location: $location", $black);
ImageString($im, $font, 15, 50, "HUB: $hub", $black);
ImageString($im, $font, 15, 65, "Hours: $hours", $black);
$font_width = ImageFontWidth(5);
ImagePng($im);
I am trying to create a simply function in php that will take some int inputs and use them to draw a rectangle but the below function doesn't work...
<?php
$img = imagecreatetruecolor(500, 500);
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
//set canvas background to white
imagefill($img, 0, 0, $white);
//THIS FUNCTION IS NOT WORKING
function draw($x1Pos, $y1Pos, $x2Pos, $y2Pos, $colour) {
imagerectangle($img, $x1Pos, $y1Pos, $x2Pos, $y2Pos, $colour);
}
draw(20, 40, 60, 80, $red);
draw(30, 40, 80, 100, $green);
imagerectangle($img, 150, 100, 300, 250, $green);
imagerectangle($img, 100, 100, 200, 200, $blue);
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
?>
Your code is failing for two reasons; firstly, $img is never passed to the function. You either need to declare it as global within the function, or pass it through a parameter.
Secondly, $blue doesn't exist. Replace that or actually write it into existence, and your code works fine.