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.
Related
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');
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 am designing my own PHP MVC
in which any Library file can be used by following steps:
Loading the File $this->registry->load->lib('Image');
Accessing Method from the file $this->registry->Image->anyMethod();
The first line loads the file Image.php located in lib folder and returns an instance
as $this->registry->Image
then using that instance, method anyMethod() from the file can be accessed as$this->registry->Image->anyMethod(); fromController
PROBLEM is that, I am not able to Output any image !
the following code do not work if accessed from Controller but works if directly used !
codes taken from http://in2.php.net/imagettftext
public function anyMethod()
{
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
}
Please Help , I am stuck.
Note: By adding ob_clean(); before imagepng($im); seems working but without text on the Image .
Add an exit; call after destroying the image to prevent any further output being added by your MVC system:
imagepng($im);
imagedestroy($im);
exit;
Also check there are no other Content-type headers being issued before your anyMethod() call.
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.
I have now spent several hours trying to figure this out.
I have this function to take an image on the server,copy it, resize it, and save it in a temporary location.
The function works and is tested inside another php file.
But THIS PHP FILE refuse to find the folder '../temp_images' (you can see it in the variable $temp_path).
I have tried all kinds of stuff, adding server root etc...
Does anybody know why this function cant find the path to the directory in this php file, but it can in another php file in the same folder?
The function I am referring to is at bottom of the code, imagejpeg();
Here is the function (shortened):
function show_pics($tot_pics, $id_string, $category){
$ad_id_stripped = end( explode( '_', $id_string ) );
$img_path="SV/main/ad_images/$category/";
$temp_path="../temp_images/remove_images/";
$maxH = 70;
$maxW = 93;
$top_offset = 0;
for ($i=1; $i<=$tot_pics; $i++){
$image_p = imagecreatetruecolor($fwidth, $blank_height);
$white = imagecolorallocate($image_p, 255, 255, 255);
imagefill($image_p, 0, 0, $white);
$image = imagecreatefromjpeg('../ad_images/'.$category.'/'.$ad_id_stripped.'_'.$i.'.jpg');
imagecopyresampled($image_p, $image, 0, $top_offset, 0, 0, $fwidth, $fheight, $width_orig, $height_orig);
imagejpeg($image_p, $temp_path, 100);
}}
Thanks
Where is this PHP file located? And you don't use $temp_path anywhere in your code? :)
http://be2.php.net/imagejpeg the second parameter should be a filename, not a directory.
This function is used to display random image i.e. at header position of a site. It reads the whole directory and then randomly print the image.
I think it may be useful............