Does anybody know what can call these files properly?
I'm trying to put text on a JPEG image, and apparently the way I'm calling those 2 files is not working
It is a PHP 7.4 Laravel 8 project and the script is located in a blade view.
<?php
// C:\xampp\htdocs\solidcadclass\public\storage\resources\arial.ttf
// C:\xampp\htdocs\solidcadclass\public\storage\resources\template.jpeg
$img = imagecreatefromjpeg('C:\xampp\htdocs\solidcadclass\public\storage\resources\template.jpeg');
$black = imagecolorallocate($img, 0, 0, 0); // OBJ, RGB
$txt = Auth::user()->name;
$font = 'C:\xampp\htdocs\solidcadclass\public\storage\resources\arial.ttf';
imagettftext(
$img, // Image object
24, // Font Size
0, // Angle
5, 24, // x,y
$black, // Color
$font, // Font to use
$txt // Text to write
);
// OUTPUT IMAGE
header("Content-type: image/jpeg");
imagejpeg($img);
I am getting bunch of weird symbols like:
����JFIF ...
Any advice on how to call files in functions like these would be really useful!
You can use Laravel response function with passing image in response parameter.
return response($img)->header('Content-type','image/jpeg');
Related
I'm needing to create on my page an image with dynamic content.
This image needs to be transparent background (PNG).
So I searched and I figure out how to do this.
But I have one more problem... My dynamic content is an image as well... for real, it's a \EmojieOne library, that takes the code like that: ":joy:" from the database and print the real emoji with:
toImage(':joy:')
So now, when I try to use this in my imagestring (to create my image), The image generated receive the code instead to receive the emoji image.
//My EmojiOne class
$client = new Client(new Ruleset());
$emojiCode = $client->toImage($emojiCode);
//Creating my image PNG
$image = imagecreatetruecolor(800, 400);
imagesavealpha($image, true);
$textcolor = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocatealpha($image, 0, 0, 0, 125);
imagefill($image, 0, 0, $color);
//Printing the dynamic content in my new image
imagestring($image, 5, 0, 0, $emojiCode, $textcolor);
imagepng($image);
Does anyone know what I can do for the new image to receive the emoji image, rather than its HTML code in a text?
I have copied the following code from somewhere in the web. It is stored in a file called captcha.php:
$md5_hash = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$security_code = substr($md5_hash, 15, 5);
//Set the session to store the security code
//Set the image width and height
$width = 100;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
ImageString($image, 3, 30, 3, $security_code, $white);
//Throw in some lines to make it a little bit harder for any bots to break
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
I need to use in the laravel. I should give the path of this php file to the src attribute of the <img> tag. When the file is located in a folder in public directory, it works, but when I do the following in a route, it does not work, and no image gets created.
I need to incorporate it into the Laravel Routes to use its Session:: capabilities.
Here is the router in which the above code is inserted:
Route::get('captcha', array('as'=>'captcha'), function(){
// above code
});
I'm not clear about your problem but you can create a class and put this code inside that class using a public method, for example, you may create a class inside your app/libs folder (create the libs folder inside app) and in this libs folder create a class similar to this:
namespace Libs\Captcha;
class Captcha {
public function dumpCaptcha()
{
// put the captcha code here but make
// changes to the last part as given below
//Tell the browser what kind of file is come in
ob_start();
header("Content-Type: image/jpeg");
ImageJpeg($image);
$img = ob_get_clean();
ImageDestroy($image);
return base64_encode($img);
}
}
In the composer.json file's autoload -> classmap section add another entry at the end like this:
"autoload": {
"classmap": [
// ...
"app/tests/TestCase.php",
"app/libs/captcha/Captcha.php",
]
}
Then run composer dump-autoload from the terminal/command prompt and then use this class inside your route like this:
Route::get('captcha', array('as'=>'captcha'), function(){
$captcha = App::make('Libs\\Captcha\\Captcha');
return View::make('view_name_here')->with('captchaImage', $captcha->dumpCaptcha());
});
Then in your view you may use something like this
<img src={{ 'data:image/jpeg;base64,' . $captchaImage }} />
The image will be displayed.
Good day everyone.
Long story short, I'm trying to make an online card game much like urban rivals.
To do so, I'm using mainly PHP and SQL.
Right now, I'm trying to generate the png file of a card based on a database.
I've already managed to merge images with alpha channel, to add text to an image, but my real problem is : I cannot seem to be able to output this image along with other and the basic layout of a HTML document. Here is my test code :
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
echo('Hello !');
?>
Sorry for the bad formatting, I haven't been able to make it better :/
Here is my function in phpGDTools.php:
function mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim)
{
//TEST FUNCTION
//Both paths to the images, x and y dimensions of the image you're about to fuse.
$final_img = imagecreate($xdim, $ydim);
//Step 1 : Create the objects from the PNGs
$image_1 = imagecreatefrompng($image1path);
$image_2 = imagecreatefrompng($image2path);
//Step 1.2 : Allow transparency of bases.
imagealphablending($image_1, true);
imagesavealpha($image_1, true);
imagealphablending($image_2, true);
imagesavealpha($image_2, true);
//Step 2 : Merge the images into one.
imagecopy($image_1, $image_2, 0, 0, 0, 0, $xdim, $ydim);
imagecopy($final_img, $image_1, 0, 0, 0, 0, $xdim, $ydim);
//Step 3 : Allow transparency of final image.
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
//Step 4 : Output image to browser.
header('Content-Type: image/png;');
imagepng($image_1);
}
This outputs very well the image, as intended, but the text "Hello !" is nowhere to be found.
Also, please ignore the part about "$final_image" it's just for testing.
You may have noticed I commented this line :
//$image = mergeTwoPNGAndReturn($image1path, $image2path, $xdim, $ydim);
This is the function I want to use : hopefully, my goal would be to include this php file into an html page, and then being able to print the image object between two tags.
Technically, I could save it as afile and then output the file, but that would be extremely inefficient considering my project.
Thank you for helping me out.
~Jules Courtois
You're setting the content type to an image, so anything you send back will be interpreted as an image. When you're echoing HTML you're adding to the image data which is resulting in the browser not being able to read the image.
You need to create a HTML page then reference images within the HTML file with a or tag with the src to the URL of the image or you can base64 an image to use it inline.
You could replace
imagepng($image_1);
with
ob_start();
imagejpeg($image_1);
$image_data = ob_get_contents ();
ob_end_clean ();
$image_data_base64 = base64_encode($image_data);
echo '<img src="data:image/png;base64,' . $image_data_base64 . '" />';
You should avoid processing images on the fly and try to store them but this should achieve what you asked in the question.
It looks like you are trying to output image data and html data from the same php script.
You call the function
mergeTwoPNGAndOutputInBrowser();
which outputs the image header to the browser.
And then you
echo('Hello !');
Where 'hello' is text data and not image data... So its not displayed in the image.
You could do something like this.
<?php
include 'phpGDtools.php';
$image1path = 'monstercard.png';
$image2path = 'monsterimage.png';
$xdim = 220;
$ydim = 301;
if(isset($_GET['display_image'])){
//Output image data
mergeTwoPNGAndOutputInBrowser($image1path, $image2path, $xdim, $ydim);
}
else{
//Output page data
echo 'Hello !';
echo "<img src=\"?display_image\">";
}
?>
The Text is not displaying since you have set the header
header('Content-Type: image/png;');
Why don't you use the imagestring() of the same PHP GD Library. It let's you write text on the image.
Here's a cool example. You can embed this same code to your mergeTwoPNGAndOutputInBrowser() function.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Source
may be you can help me.
I need a function to draw polyline path from _GET or _POST string and save generated image to the folder.
For example my link will looks like: http://img.domain.com/?points = 1,5,-70,300,250,500...
If image already generated and do not changed -> load it from folder. Else generate new one.
My code here:
if (isset($_POST['points'])) {
$points = $_POST['points'];
$image = imagecreate(200, 200);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
... polyline path drawing here...?
imageline($image, 10, 10, 10, 190, $black);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
... how to save it to the server?
}
Thanks.
To save the image you can use the second (optional) parameter of imagepng:
imagepng($image, 'saved.png');
For the polyline you will be calling imageline inside a loop -- exactly how depends on what your $points value is structured.
To save an image to the server on the fly, use the image function's second parameter to specify a location and filename.
//specify the path on the server where you want to save the image
$path_image = 'saved-example.png';
imagepng($image, $path_image);
imagepng($image);
imagedestroy($image);
Image will be saved to that path.
I have a code that resize and colorize the image accordingly input values... the problem is I can able to colorize only one time with fresh image saved by other application..Please help me.. I hope there are many PHP expers are here.....
<?php
createImage(50,50, 0,0, 255);
function createImage($width, $height, $nR, $nG, $nB)
{
$image = imagecreatefrompng("source.png");
imagealphablending($image, false);
imagesavealpha($image, true);
//resize the image
$new_image = imagecreatetruecolor($width, $height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, imagesx($image), imagesx($image));
//colorize the image
$nrgb = str_pad(dechex($nR), 2, '0', STR_PAD_LEFT). str_pad(dechex($nG), 2, '0', STR_PAD_LEFT). str_pad(dechex($nB), 2, '0', STR_PAD_LEFT);
$newColor = $nrgb;
$c2 = sscanf($newColor ,"%2x%2x%2x");
for($i=0;$i<$width;$i++)
{
for($j=0;$j<$height;$j++)
{
$cIndex = imagecolorat($new_image,$i,$j);
imagecolorset($new_image,$cIndex,$c2[0],$c2[1],$c2[2]);
}
}
header("Content-Type: image/png");
imagepng($new_image,"test.png");
}
?>
Sounds to me like you are manipulating an image resource and outputting it and then wanting to go back and further manipulate it without starting over. You can do this by
a) save the image resource as a session variable, and then use the session variable in subsequent alterations.
b) save the altered image before outputting it, and then open the saved altered image and go from there. I don't know what file type you are using but for instance with gif images your code should be using imagegif() to output the image. You would utilize this same function (or other image type equivalent function) to also save the image.
I suggest looking at the imagefilter function found here: http://php.net/manual/en/function.imagefilter.php
Look at IMG_FILTER_COLORIZE on that page.