php drawing function - php

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.

Related

create shape not working in laravel (imagecreatetruecolor )

I am trying to create few shapes like circle, rectangle in PHP laravel. My program working fine in a normal PHP file. But whenever I am trying to add this code in laravel its returns a special character.
geometric.blade.php
<?php
header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200, 200);
// Allocate colors
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
// Draw three rectangles each with its own color
imagerectangle($canvas, 50, 50, 150, 150, $pink);
imagerectangle($canvas, 45, 60, 120, 100, $white);
imagerectangle($canvas, 100, 120, 75, 160, $green);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($canvas);
imagedestroy($canvas);
?>
Error output in laravel
����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ��C   $.' ",#(7),01444'9=82<.342��C  2!!22222222222222222222222222222222222222222222222222����"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?���(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��+cA�l�5�翽�����\�Al'v̱�)te9�#�[+ 7L'I����ݥ�I�l��H��"�aGj��V���o����㵻�ZxL�h���h(�m�iG�/'�'���s���c��u��C��T�6��v^]e�S�ճ����.?��ڶ��������c��u��C��T}�����?�M�%Qr}����ȧ��g�# 7��\��?�l�����ˏ�;W>������&��������;����J���e�%�E?�[?�i���������g�# 7��\�ڹ�?��w\��4?��G���A�s����Uk/�/�)�j���M����v��[?�i�������ϱ�?����� �����t��O�zO��}��[o7n����d�8�2h�{YI�'����4���q��h�ճ����.?��e�E����K��� �u��m�/�[�H��r�D�n���:���M�B2�ו����Yt1�� �?
Please let me know how to run this in Laravel framework. Thanks!
You can not use header on blade/view. You should create it on controller/route directly.
example on route:
Route::get(
'/test/img',
function () {
header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200, 200);
// Allocate colors
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
// Draw three rectangles each with its own color
imagerectangle($canvas, 50, 50, 150, 150, $pink);
imagerectangle($canvas, 45, 60, 120, 100, $white);
imagerectangle($canvas, 100, 120, 75, 160, $green);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($canvas);
imagedestroy($canvas);
}
);
Content-type header in view/blade will be replaced by default. But if you still want use blade actually you can use die function in the end of blade file.
Example on blade
<?php
header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200, 200);
// Allocate colors
$pink = imagecolorallocate($canvas, 255, 105, 180);
$white = imagecolorallocate($canvas, 255, 255, 255);
$green = imagecolorallocate($canvas, 132, 135, 28);
// Draw three rectangles each with its own color
imagerectangle($canvas, 50, 50, 150, 150, $pink);
imagerectangle($canvas, 45, 60, 120, 100, $white);
imagerectangle($canvas, 100, 120, 75, 160, $green);
// Output and free from memory
header('Content-Type: image/jpeg');
imagejpeg($canvas);
imagedestroy($canvas);
die;

Text is disappearing when I try to add an image in PHP + GD library

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();
?>

Transparent Overlay Creating PNG

I have the below function and I've gotten it to the size I need, now I'm adding a background image and then looking to add just text overlay with a transparent background on all the overlay text but imagefilledrectangle applies the variable $white which is expected, I'm trying to see if there is anyway to tell the function to be transparent instead of accepting a colour, white in this instance, the PHP docs say that the colour is a required parameter for imagefilledrectangle, any suggestions greatly appreciated.
The background image is a local image:
define("BACKGROUND_FILE", "background.png");
Function:
public function generateImage()
{
if (file_exists(BACKGROUND_FILE)) {
$im = #imagecreatefrompng(BACKGROUND_FILE);
if($im) {
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
$text = 'Test';
$font = 'arial_narrow_7.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
} else {
echo 'Error';
}
}
}
I tried adding imagecolortransparent($im, $white); before imagefilledrectangle but it didn't make any difference.
Figured it out, there is no need to have the imagefilledrectangle line in the function, the below gives transparent overlay text elements as no fill is now being applied.
public function generateImage()
{
if (file_exists(BACKGROUND_FILE)) {
$im = #imagecreatefrompng(BACKGROUND_FILE);
if($im) {
$black = imagecolorallocate($im, 115, 150, 195);
$text = 'Test';
$font = 'arial_narrow_7.ttf';
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im);
} else {
echo 'Error';
}
}
}

Get variable used to create image

I'm making my own 'captcha' form and I now have a page that generates the image:
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(200, 50);
$white = imagecolorallocate($im, 255, 255, 255);
$gray = imagecolorallocate($im, 160, 160, 160);
imagefilledrectangle($im, 0, 0, 200, 50, $white);
$captcha = "SOMErandomTEXT";
$font = 'Chewy.ttf';
imagettftext($im, 20, 0, 0, 20, $gray, $font, $captcha);
imagepng($im);
imagedestroy($im);
?>
Now I also have another page that shows this image, inside the form. Now I want to get the value $captcha from the page shown above on the other page. How can I do it?
Using sessions did the job for me.
PHP Sessions

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

Categories