Drawing shapes on image and saving it in PHP - php

I am trying to read an image from a location in my server and then draw lines on it and then overwrite the image in the location.
My code is as follows:
function drawShapes($src_path, $json)
{
echo "---inside draw Sharpes-------";
$x1= $json['x1'];
$y1= $json['y1'];
$x2= $json['x2'];
$y2= $json['y2'];
$x3= $json['x3'];
$y3= $json['y3'];
$x4= $json['x4'];
$y4= $json['y4'];
$type = exif_imagetype($src_path);
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($src_path);
break;
case 2 :
$im = imageCreateFromJpeg($src_path);
break;
case 3 :
$im = imageCreateFromPng($src_path);
break;
}
if (!$im)
return false;
imagesetthickness($im, 5);
$color = imagecolorallocate($im, 255, 255, 255);
echo $color;
imageline($im, $x1, $y1, $x2, $y2, $color);
imageline($im, $x2, $y2, $x3, $y3, $color);
imageline($im, $x3, $y3, $x4, $y4, $color);
imageline($im, $x4, $y4, $x1, $y1, $color);
header("Content-type: image/jpeg");
imagejpeg($im,$src_path);
imagedestroy($im);
}
Here $src_path= "uploads/case.jpg"- uploads is a folder inside my solution & case.jpg is the image file name.
But I am getting image missing icon as output. What is the mistake I do?
What is the solution to it? Thank you.

You are echoing data before you send the headers.
If you want to overwrite the image in the location and then display it in the browser, maybe you can work with this adjustment:
<?php
function drawShapes($src_path, $json)
{
//echo "---inside draw Sharpes-------";
$x1= $json['x1'];
$y1= $json['y1'];
$x2= $json['x2'];
$y2= $json['y2'];
$x3= $json['x3'];
$y3= $json['y3'];
$x4= $json['x4'];
$y4= $json['y4'];
$type = exif_imagetype($src_path);
$allowedTypes = array(
1, // [] gif
2, // [] jpg
3, // [] png
);
if (!in_array($type, $allowedTypes)) {
return false;
}
switch ($type) {
case 1 :
$im = imageCreateFromGif($src_path);
break;
case 2 :
$im = imageCreateFromJpeg($src_path);
break;
case 3 :
$im = imageCreateFromPng($src_path);
break;
}
if (!$im)
return false;
imagesetthickness($im, 5);
$color = imagecolorallocate($im, 255, 255, 255);
//echo $color;
imageline($im, $x1, $y1, $x2, $y2, $color);
imageline($im, $x2, $y2, $x3, $y3, $color);
imageline($im, $x3, $y3, $x4, $y4, $color);
imageline($im, $x4, $y4, $x1, $y1, $color);
imagejpeg($im,$src_path);
imagedestroy($im);
$fp = fopen($src_path, 'rb');
header("Content-Type: image/jpeg");
header("Content-Length: " . filesize($src_path));
fpassthru($fp);
exit;
}

Related

creating captcha image in php when uploaded to server

using the code below to ceate image used in captch, it works fine in localhost but when i upload to server no image is rendered.
<?php
require_once 'core/init.php';
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 30;
$image_width = 120;
$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<=299; $x++) {
$x1 = rand(1, 999);
$y1 = rand(1, 999);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 33, $text_color, 'fonts/font.TTf', $text);
imagejpeg($image);

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 draw semi-transparent rectangle in php?

Here is an example what I would like to do:
Here is the result:
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 50)
{
// Load image
$img = imagecreatefromjpeg($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
// Save the image (overwrite)
imagejpeg($img, $img_src);
imagedestroy($img);
}
You need to use http://php.net/manual/en/function.imagefilledrectangle.php, passing a color created with http://www.php.net/manual/en/function.imagecolorallocatealpha.php.
As you can see, the example for http://php.net/manual/en/function.imagefilledrectangle.php is pratically what to you want to do.
function red_rectangle($img_src,$x1,$y1,$x2,$y2,$tr = 100)
{
// Load image
$img = imagecreatefromjpeg($img_src);
// Transparent red
$red = imagecolorallocatealpha($img, 255, 0, 0, $tr);
// Draw a white rectangle
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
// Don't forget to output a correct header
header('Content-Type: image/jpg');
// Save the image (overwrite)
imagejpeg($img);
imagedestroy($img);
}
$img_src = 'test.jpg';
$x1= 500;
$y1= 450;
$x2 = 370;
$y2=180;
red_rectangle($img_src,$x1,$y1,$x2,$y2);

Generate and then save an image

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.

Identifying Specific Characters from Text Input

Original title:
to read and count the occurrence of only limited characters of A,G,C,T present in the input text file, for example only 100 out of 500 and draw a thin verticular rectangle barcode images using gd
It read and counts only first 10 characters and draw a barcode image, instead of specified 100 characters.
<?php
$file="co3.txt";
$handle=fopen($file, 'r');
$A=0;
$G=0;
$C=0;
$T=0;
$img = imagecreate(850,80);
$white = imagecolorallocate($img, 255,255,255);
$green=imagecolorallocate($img, 0, 128, 0);
$black=imagecolorallocate($img, 0, 0, 0);
$red=imagecolorallocate($img, 255, 0, 0);
$blue=imagecolorallocate($img, 0, 0, 255);
$x1=40;
$y1=40;
$x2=43;
$y2=80;
$contents = '';
#while(( ($contents=fread( $handle, 100)) !='')) {
while(( ($contents=fread($handle, 100)) )) {
for ($i=0; $i<=100; $i++)
{
if($contents[$i] == 'A')
{
$A++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $green);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'G')
{
$G++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $black);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'C')
{
$C++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $blue);
$x1 = $x1+6;
$x2 = $x2+6;
}
else
if($contents[$i] == 'T')
{
$T++;
imagefilledrectangle($img, $x1, $y1, $x2, $y2, $red);
$x1 = $x1+6;
$x2 = $x2+6;
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
}
}
}
?>
I'm guessing you only not so much the first 10 letters, but up to the point when a T is found. Then, the misplaced snippet producing the image ouptut this image. The loop may continue up to 100, producing possibly a few more images (with each new T), but I don't know PHP behavior in such cases, since some of the HTTP response is readily written...
Alternatively you may be seeing the last of these images. At any rate, by fixing the misplaced
header("Content-type: image/png");
imagepng($img);
imagedestroy($img);
things should start to look more as expected.

Categories