php gd function to draw polyline from array - php

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.

Related

Laravel Image creation code

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.

Using image create function

I'm using Creating image function to create an image with exact text message using the following code
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
The output should be like this
Now my question is there any way i can write image over it not only text
so that if i've flag image at same path (flag.gif ) and i would like to write it just after my $message to be like this
so is this possible and how could be ! ~ thanks a lot
Update
based on #MarcB idea of using imagecopy function
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromgif("myimage.gif"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
the output is not true color WHY :(
ANY help about this new problem ~ thanks
With the help of Surreal Dreams and Marc B
This one works fine
<?PHP
header ("Content-type: image/gif");
$image=imagecreatefromjpeg("myimage.jpg"); // will be background img
$src = imagecreatefromjpeg('flag.jpg');// new image will add
imagecopy($image, $src, 120, 10, 0, 0, 32, 20);
$black = imagecolorallocate($image, 0,0,0);
$message = "Hello Egypt";
imagestring($image, 4, 25, 10, $message, $black);
imagegif($image);
imagedestroy($image);
?>
Output
I've should have learned the following functions
imagecopy
imagecreatefromjpeg
imagecreatefromgif
imagecopy() should properly deal with differences between images' palettes; however, the GIF format does not support more than 256 colors, and neither does GD when it works with palette-based images. If 256 palette entries already exist when GD tries to use a new color, GD will pick the closest match, which can produce the results you see.
To avoid this problem, you should use imagecreatetruecolor() to create a 24-bit true-color image in memory. You can then use imagecopy() to insert each GIF image (including the background) and imagepng() to generate PNG output, which is better for line art than JPEG, offers better compression than GIF, and can support more than 256 colors.

Merge two images in php

I have two images I would like to merge then save to a new location.
I would like the second image to be place directly below the first image.
I have the following so for but the image doesn't even save.
$destimg = imagecreatefromjpeg('images/myimg.jpg');
$src = imagecreatefromgif('images/second.gif');
// Copy and merge
imagecopymerge($destimg, $src, 316, 100, 0, 0, 316, 100, 100);
Both images have a width or 316px X 100px
From the above code the $destimg should now be 316x200 but that doesn't happen. Also like it to be a new image and save to another folder.
Thanks for any help.
The best approach for this situation may be to create a new image in memory with the combined dimensions you desire, then copy or resample the existing images to the new image, and then save the new image to disk.
For example:
function merge($filename_x, $filename_y, $filename_result) {
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
// Create new image with desired dimensions
$image = imagecreatetruecolor($width_x + $width_y, $height_x);
// Load images and then copy to destination image
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);
// Save the resulting image to disk (as JPEG)
imagejpeg($image, $filename_result);
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
Example:
merge('images/myimg.jpg', 'images/second.gif', 'images/merged.jpg');
i would like to add one more thing here if you are using PHP GD Library,then you should include imagesavealpha() and alphablending() also.
I suggest you to use Image Magick instead (pecl-imagick module or running it as command via shell). I have several reasons:
Imagick is:
faster
knows more format
makes better quality images
have more ability (for example text rotation)
and more...
Your method is Imagick::compositeImage if you use the php module. Manual: http://php.net/manual/en/function.imagick-compositeimage.php
I found the answer, use GD:
function merge($filename_x, $filename_y, $filename_result) {
// Get dimensions for specified images
list($width_x, $height_x) = getimagesize($filename_x);
list($width_y, $height_y) = getimagesize($filename_y);
// Create new image with desired dimensions
$image = imagecreatetruecolor($width_x, $height_x);
// Load images and then copy to destination image
$image_x = imagecreatefromjpeg($filename_x);
$image_y = imagecreatefromgif($filename_y);
imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
// top, left, border,border
imagecopy($image, $image_y, 100, 3100, 0, 0, $width_y, $height_y);
// Save the resulting image to disk (as JPEG)
imagejpeg($image, $filename_result);
// Clean up
imagedestroy($image);
imagedestroy($image_x);
imagedestroy($image_y);
}
like this:
merge('images/myimage.jpg', 'images/second.gif', 'images/merged.jpg');

How to draw a png image in php?

$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
I have already draw a black image i want draw a file suppose 3.png on it ..
How to do that ?
You have to load an image you want to draw and then use imagecopy() to draw it:
// the part you already have; creates 538x616 px black image
$image = imagecreatetruecolor(538,616);
$black = imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$black);
// load image from file and draw it onto black image;
// for loading PNG, use imagecreatefrompng()
$overlayImage = imagecreatefromjpeg('macro_photo_1.jpg');
imagecopy($image, $overlayImage, 10, 10, 0, 0, imagesx($overlayImage), imagesy($overlayImage));
// send image to the browser
header("Content-Type: image/png");
imagepng($image);
exit;
I would also advise to go through the list of GD and Image functions to see what (and how) can be done with images in PHP.
imagepng($image);
And if you haven't already you need:
header('Content-Type: image/png');
Before you call imagepng to let the browser know it's looking at an image not an HTML page.
Saroj, http://www.php.net/manual/en/function.imagecreate.php and from same page, here is the example snippet
<?php
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
HTH!

how to colorize PHP result image using GD

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.

Categories