how to draw semi-transparent rectangle in php? - 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);

Related

Drawing shapes on image and saving it in 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;
}

Center watermark text on image when unknown size of text

I'm trying to create an image with a watermark text in it and I want it to be central on the image. The text I want to watermark can be anywhere from 5 characters to 15 characters, therefore I cannot put a final size for the text to fit every image.
This is the code I use to create the watermarked image
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = '../fonts/proxima-nova-light.otf';
$font_size = 100;
imagettftext($image_p, $font_size, 0, 303, 190, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
Which does an excellent job on some texts but when I try it on longer texts it looks bad.
I want to - somehow - calculate the optimal size of text and choose size, x and y from there.
Any ideas?
After doing some research with help from #Sammitch I was able to figure it out. Here is the working code:
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
$font = 'fonts/your-font-here.ttf';
$font_size = 40;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$bbox = imagettfbbox($font_size, 0, $font, $WaterMarkText);
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) + 10;
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image_p, $font_size, 0, $x, $y, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
Calculate the position of the watermark in the output image (the
watermark shall be placed in the Center of the Image)
$watermark_pos_x = (imagesx($image)/2) - (imagesx($watermark)/2) - 15;
$watermark_pos_y = (imagesy($image)/2) - (imagesy($watermark)/2) - 10;
Use this value on 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"

center text in image

So I have an image, and I am writing text and a color box onto the image. It works but it's being added to the image in the top right corner, but I need it in the center of the image. I tried changing the x and y variables, but it only moves the text and not the white box.
Here is code
$image_filepath = './kenshin.jpg';
saveImageWithText("Welcome to Eureka!", $color, $image_filepath);
function saveImageWithText($text, $color, $source_file) {
$public_file_path = '.';
// Copy and resample the imag
list($width, $height) = getimagesize($source_file);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($source_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
// Prepare font size and colors
$text_color = imagecolorallocate($image_p, 0, 0, 0);
$bg_color = imagecolorallocate($image_p, 255, 255, 255);
$font = $public_file_path . '/arial.ttf';
$font_size = 12;
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
// Get the size of the text area
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
// Add text
imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
// Save the picture
imagejpeg($image_p, $public_file_path . '/output.jpg', 100);
// Clear
imagedestroy($image);
imagedestroy($image_p);
};
Here is output
Try this. It will help you…
<?php
$img = imagecreatefromjpeg("girl-hugging-the-globe.jpg"); // image.jpg is the image on which we are going to write text ,you can replace this iamge name with your
if(!$img) die("Unabe to load image");
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$width = 600;// it will store width of image
$height = 100; //it will store height of image
$fontsize = 6; // size of font
$text = "Block Prints Online"; // Define the text
$pos = ( $width - imagefontwidth($fontsize)*STRLEN($text) );// calculate the left position of the text:
imagestring($img, $fontsize, 200, 150, $text, $red);
header('Content-type: image/jpeg');
imagejpeg($img);//it will output a jpeg image
imagedestroy($img);//it will destroy $img*/
?>

PHP GD ttftext center alignment

I'm using imagettftext to make a bar graph and at the top of each bar I want to put the value.
I have the following variables for each bar (which are really rectangles)
$x1
$y1
$x2
$y2
$imagesx
$imagesy
$font_size
Also, The fontsize should decrease as the string length increases.
Do it like this. Remember to place the font file "arial.ttf" in current directory:
<?php
// Create a 650x150 image and create two colors
$im = imagecreatetruecolor(650, 150);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// Set the background to be white
imagefilledrectangle($im, 0, 0, 649, 149, $white);
// Path to our font file
$font = './arial.ttf';
//test it out
for($i=2;$i<10;$i++)
WriteTextForMe($im, $font, str_repeat($i, $i), -140 + ($i*80), 70 + rand(-30, 30), -160 + (($i+1)*80), 150, $black);
//this function does the magic
function WriteTextForMe($im, $font, $text, $x1, $y1, $x2, $y2, $allocatedcolor)
{
//draw bars
imagesetthickness($im, 2);
imagerectangle($im, $x1, $y1, $x2, $y2, imagecolorallocate($im, 100,100,100));
//draw text with dynamic stretching
$maxwidth = $x2 - $x1;
for($size = 1; true; $size+=1)
{
$bbox = imagettfbbox($size, 0, $font, $text);
$width = $bbox[2] - $bbox[0];
if($width - $maxwidth > 0)
{
$drawsize = $size - 1;
$drawX = $x1 + $lastdifference / 2;
break;
}
$lastdifference = $maxwidth - $width;
}
$size--;
imagettftext($im, $drawsize, 0, $drawX, $y1 - 2, $allocatedcolor, $font, $text);
}
// Output to browser
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
It uses imagettfbbox function to get width of the text, then loops over the font size to get correct size, centers it and displays it.
So, it outputs the following:
You can calculate the center of your text by using PHP's imagettfbbox-function:
// define text and font
$text = 'some bar label';
$font = 'path/to/some/font.ttf';
// calculate text size (this needs to be adjusted to suit your needs)
$size = 10 / (strlen($text) * 0.1);
// calculate bar center
$barCenter = $x1 + ($x2 - $x1) / 2;
// calculate text position (centered)
$bbox = imagettfbbox($size, 0, $font, $text);
$textWidth = $bbox[2] - $bbox[0];
$positionX = $textWidth / 2 + $barCenter;
$positionY = $y1 - $size;
EDIT: Updated the code to do all the work for you.

Categories