How to draw a vertical line on image in php [duplicate] - php

This question already has answers here:
PHP GD how to draw text over a line
(3 answers)
Closed 9 years ago.
i am making an antibot that displays different pictures, and asks the user to click on a specific picture. However, i would like some small random change to be made to the picture each time, so that software could not analyze and determine which picture is beeing displayed...
I would like one horizontal and one vertical line to be added at random coordinates with random color to the picture each time, then display the picture using get_file_contents and header.
Hope this makes sense... I would not want the changes to be saved to the picture, but only displayed to the user... I am using file_get_contents and header to display the picture, like this:
$id = $_GET['id'];
$image = "images/".$id . ".jpg";
$content = file_get_contents($image);
header('Content-Type: image/jpeg');
echo $content;
exit()
Thanks...

Using GD within PHP would allow you to do this
http://php.net/manual/en/book.image.php
create a file called image.php
From the php manual:
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
You could add a line by using the imageline function
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imageline($im, $x1, $y1, $x2, $y2, $orange);
imagepng($im);
imagedestroy($im);
?>

There are so many PHP GD functions available to achive this I think below code snippet may help you
<?php
$im = imagecreatefrompng("images/yourImage.png");
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
// Draw a vertical dashed line
imagedashedline($im, 50, 25, 50, 75, $white);
// Save the image
imagepng($im, './imagewithdashedline.png');
imagedestroy($im);
?>
reference PHP GD imagedashedline function

Related

Can't show image with GD library PHP

I'm trying to display an image from another server with the code below
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("https://i.imgur.com/aAnTsd8.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
The above code works fine, but when I change the URL of the image to another URL, example that I put below, the code does not display the image.
https://www.wordapp.com/wpsite/wp-content/uploads/2017/01/google-ranking-factors.png
Does anyone know the reason for displaying one image and not displaying the other?
Thank you

Creating image and loading it in the page

I want to create/generate an image with PHP, adding text into it, but without saving it into the FTP, I'd like to load it on the site by changing the content type to image/png, the same way I did it with ASP.NET:
Response.ContentType = "image/png";
rImage.Save(Response.OutputStream, ImageFormat.Png);
I found a function called file_put_contents, but I'm not so sure this is what I'm looking for. If you know how to do something like this, please tell me.
I'm trying this code, but it fails to load the image, and shows the browser's default error image.
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
Below example will regenerate an image with text over it:
<?php
header("Content-type: image/png"); // Set the header so that browser can output the image
$string = 'Print this text on Image'; // Text to be added on the image
$im = imagecreatefrompng("images/button1.png"); // Take base image
$orange = imagecolorallocate($im, 220, 210, 60); // Allocate a color for an image
imagestring($im, 3, 0, 0, $string, $orange); // Write the string at the top left
imagepng($im); // Create new image
imagedestroy($im); // destroy object
?>
Reference: http://php.net/manual/en/image.examples-png.php

Add Dynamic Text to images using GD Library or any other plugin in PHP

I want to make an app where user can see their meaning of name. I am done with the php part its just I am stuck how to output it to good looking image. I have used PHP image magician library before. But I am still not able to get it in the format I wanted.
Any help will be useful.
CHECK the image format below
https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDPCj8lZUELWsXD&w=470&h=246&url=http%3A%2F%2Fs3.alegra.me%2Fname%2Fnombre.php%3Fnombre%3Djayanta&cfs=1&upscale
$im = imagecreatefromjpeg('xc7ci4ptuxmgy3nd0brz.jpg');
$font="tahoma.ttf";
$red = imagecolorallocate($im, 255,0,0);
$string="JAYANTA";
$last_string= substr( $string, 1 );
$temp_x = 105;
$down_string[0]="JUST";
$down_string[1]="AMUSING";
$down_string[2]="YOUNG";
$down_string[3]="AMUSING";
$down_string[4]="NICE";
$down_string[5]="TOUGH";
$down_string[6]="AMUSING";
$bbox = imagefttext($im, 80, 0,90, 190, $red, $font, $string);
$temp_y=205; //y cord. of $down_string
for($x=1;$x < count($down_string);$x++){
for($y=0;$y < count($down_string);$y++){
$bbox = imagefttext($im, 12, 0, $temp_x, $temp_y, $red, $font, $down_string[$y][$x]);
$temp_x += 56+$y*1.8;
}
$temp_x = 105;
$temp_y+=15;
}
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
As you can see, ther is a lot of problems, but the solution is in the imagefttext function.

Using image create function

I'm using Creating image function to create an image with exact text message using the following code
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
The output should be like this
Now my question is there any way i can write image over it not only text
so that if i've flag image at same path (flag.gif ) and i would like to write it just after my $message to be like this
so is this possible and how could be ! ~ thanks a lot
Update
based on #MarcB idea of using imagecopy function
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
the output is not true color WHY :(
ANY help about this new problem ~ thanks
With the help of Surreal Dreams and Marc B
This one works fine
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromjpeg("myimage.jpg"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
Output
I've should have learned the following functions
imagecopy
imagecreatefromjpeg
imagecreatefromgif
imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.
To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.

How to draw a png image in php?

$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!

Categories