Generate and then save an image - php

I just wrote this code to generate an image
<?php
$canvas = imagecreatetruecolor(800, 350);
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
imagefill($canvas, 0, 0, $white); // BACKGROUND
function drawlinebox($x1, $y1, $x2, $y2, $height, $color){
global $canvas;
imagesetthickness ( $canvas, 1 );
for ($i=1; $i < $height; $i++){
imageline( $canvas, $x1, $y1, $x2, $y2, $color );
$y1++; $y2++;
}
}
drawlinebox(20, 20, 780, 300, 30, $green);
drawlinebox(20, 300, 780, 20, 30, $pink);
// Output and free from memory
header('Content-Type: image/png');
imagepng($canvas, NULL, 9);
imagedestroy($canvas);
?>
but I also want this image to saved automatically on the server.
Think of it as a cron job. Which creates images, then saves the image on the sever for later use and inserts the saved image location in the DB.

As stated in the manual, imagepng()'s second parameter is $filename which allows you to store the result in a file.

Related

Captcha validation in PHP

I used the following code to create Captcha to my form. Captcha creating well. Now I want to change font-size, and Font character spaces. I don't know how to change in the following code.
<?php
session_start();
$code= substr(str_shuffle("abcdefghijklmnopqrstuvwxyz"), 0, 6);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(150, 35);
$bg = imagecolorallocate($im, 255, 255, 255);
$fg = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 5, 5, $bg);
imagestring($im, 5, 8, 8, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
You can use the code below to implement a simple captcha using gd library of PHP. As you are beginner, here is a sample code for quick testing, it covers font-sizing also:
<?php
session_start();
header('Content-type: image/jpeg');
$text = rand(1000, 9999);
$font_size = 30;
$image_width = 200;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<=40; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'FREESCPT.ttf', $text);
imagejpeg($image);
?>
In imagettftext function:
imagettftext($image, $font_size, $angle, $x, $y, $text_color, '$font-family', $text);
$image is the $imagecreate function
$font_size is the size of font you want.
$angle is the angle of the fonts tilted
$x and $y are coordinates.
$text_color is the imagecolorallocate function
$font-family is the family of font you want to use
$text is the text or random text to be displayed
Here is the good tutorial on how to build captcha in php -> link
you can change font using imagestring function

PHP does not load ttf

This Script work on localhost (Windows), but not on the Server (Debian):
I have uploaded the files 1:1. Please help me, Thanks :)
<?php
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr, $user)
{
// Load image
$img = imagecreatefrompng($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 0, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
$white = ImageColorAllocate ($img, 255, 255, 255);
ImageTTFText ($img, 35, 0, 15, 350, $white, "Big.ttf",
$user);
// Don't forget to output a correct header
header('Content-Type: image/jpg');
// Save the image (overwrite)
imagepng($img);
imagedestroy($img);
}
$img_src = 'playerimages/streamnew.png';
$box = imagettfbbox ( 35 , 0 , "Big.ttf" , $_GET['username'] );
$width = abs($box[4] - $box[0]);
$height = abs($box[5] - $box[1]);
$x1= 10;
$y1= 310;
$x2 = 25+$width;
$y2=360;
red_rectangle($img_src,$x1,$y1,$x2,$y2,50, $_GET['username']);
?>
THE ANSWER: $_SERVER['DOCUMENT_ROOT']."/big.ttf"

How to show image that was edited with PHP

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

imagettftext function not working in php

I have the following code for my captcha. The lines in the image are generated but the 4 digit number is not displayed. I assume there is some problem in
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
function of code. I tried setting the path to the font file using $_server['DOCUMENT_ROOT']; yet didn't work.
here's the code:
<?php
session_start();
header('Content-type: image/jpeg');
$text= $_SESSION['secure'];
$font_size =30;
$image_width= 110;
$image_height=40;
$image=imagecreate($image_width,$image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for($x=1; $x<=30; $x++){
$x1 = rand(1,100);
$x2 = rand(1,100);
$y1 = rand(1,100);
$y2 = rand(1,100);
imageline($image, $x1, $x2, $y1, $y2, $text_color);
}
imagettftext($image,$font_size,0,15,30,$text_color,'font.TTF',$text);
imagejpeg($image);
?>
Below code Perfectly working in my pc
Remember font path should be correct.
output-
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

php drawing function

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.

Categories