editing text in image and sending by email - php

I am inserting texts in a jpg image, which serves as a template.
$jpg_image = imagecreatefromjpeg('images/receipt.jpg');
if ($jpg_image) {
$color = imagecolorallocate($jpg_image, 0, 0, 0);
imagestring($jpg_image,5,570,40,$rn,$color );
imagestring($jpg_image,5,570,110,$tq,$color );
imagejpeg($jpg_image,$filename);
}
I wish to save this edited image temporarily and then send it as an attachment by email. How do I save this file in the local folder (server) and attach in pear mail?

For save image to your local folder you need to pass second parameter as path will file name. For example:
$jpg_image = imagecreatefromjpeg('images/receipt.jpg');
$save_path = "PATH_TO_YOUR_DIR/filename.jpg";
if ($jpg_image) {
$color = imagecolorallocate($jpg_image, 0, 0, 0);
imagestring($jpg_image,5,570,40,$rn,$color );
imagestring($jpg_image,5,570,110,$tq,$color );
imagejpeg($jpg_image,$save_path);
}
Then you can attach this image in email. There is already lots of solutions for how to attach image in mail.
After attach if you want to remove image then simply unlink it
unlink($save_path);

Related

My php Watermark function is not working for png image

I am using a PHP function to add my logo as the watermark on images uploaded on my website. But I don't know why my watermark function is not working for png files. however, it works for jpeg files perfectly. this is my PHP function.
function watermark($img) {
global $wm_file, $wm_right, $wm_bottom;
// image values pulled from config.inc.php
$logo = './images/' . $wm_file; // path to the watermark.png
$sp = $wm_right; // spacing from right side
$sq = $wm_bottom; // spacing from bottom
$size = getImageSize($img);
$sizel = getImageSize($logo);
$imgA = imageCreateFromJpeg($img);
imageAlphaBlending($imgA, TRUE);
if($sizel[0] > $size[0] || $sizel[1] > $size[1])
{
// logo size > img size
$sizelo[0] = $sizel[0];
$sizelo[1] = $sizel[1];
$sizel[0] = ($sizel[0]/2);
$sizel[1] = ($sizel[1]/2);
}
else
{
$sizelo[0] = $sizel[0];
$sizelo[1] = $sizel[1];
}
$imgBa = imageCreateFromPng($logo);
$imgB = imageCreateTrueColor($sizel[0], $sizel[1]);
imageAlphaBlending($imgB, TRUE);
imageCopyResampled($imgB, $imgBa, 0, 0, 0, 0, $sizel[0], $sizel[1], $sizelo[0], $sizelo[1]);
imageColorTransparent($imgB, ImageColorAllocate($imgB, 0, 0, 0));
$perc = 100;
imageCopymerge($imgA, $imgB, ($size[0]-$sizel[0]-$sp), ($size[1]-$sizel[1]-$sq), 0, 0, $sizel[0], $sizel[1], $perc);
unlink($img);
if(imageJpeg($imgA, $img, 100))
{
imageDestroy($imgB);
imageDestroy($imgA);
return true;
}
chmod($img, 0777);
}
The problem I see is that you are using imageCreateFromJpeg() as the way to generate the resource for your $img that you are passing to the function.
If you pass a jpeg through the function it will work. If you pass a png it will not.
I recommend using imagecreatefromstring() to create all your resources as it is not dependent on the file type. Like so:
$source = imagecreatefromstring(file_get_contents($filePath));
Another benefit of this is that it will return false if the function fails to create a resource from the file path that you supplied meaning that the file is not an image file.
Now that you have a resource to use for the rest of your code, imageJpeg() will save the resource as a jpeg back to the file path.
Hope that helps.
One other side note. If you intend on using bmp images, the GD library does not have a built in function for bmps. However on PHP.net, someone did write a createimagefromBMP() that works really well. Also I think that on the latest version of PHP the GD library does now actually have a createimagefromBMP() function.
I also see that you are using unlink() to delete the image from your directory. This is not necessary for two reasons. The imageJpeg() will just overwrite the original. Also, if for some reason your script fails it may delete the image prematurely and you will loose the image without the new one being written.
Please be careful when using chmod(), always make sure that you set permissions back to the original permissions when you are done.
chmod($img, 777); //Give broad permissions.
//Do something.
chmod($img, 600(or whatever they were)); //Reset permission back to where they were before you changed them.

Creating a QR Code with a centered Logo in PHP with PHP QR Code Generator

I'm creating QR codes with PHP QR Code (http://phpqrcode.sourceforge.net/). It works well but now I need a free space for a custom graphic or logo in the center of it. And I want to do this without saving the image on the server. Has anyone a suggestion? What I've got so far is this:
<?php
$param = $_GET['projectid'];
$divider = ",";
$codeText = 'Projectname'.$divider.$param;
// outputs image directly into browser, as PNG stream
//QRcode::png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
QRcode::png($codeText, false, QR_ECLEVEL_H, 9, 2, true );
?>
Ok, I've found a solution. Where a image file is created temporaray to insert the the logo or whatever you want. I't just a very small change form the code found here http://ourcodeworld.com/articles/read/225/how-to-generate-qr-code-with-logo-easily-in-php-automatically
I'm using readfile() at the end to push everything directly to the output buffer.
<?php
// user input
$param = $_GET['projectid'];
$divider = ",";
// Path where the images will be saved
$filepath = 'content/images/qr/qr-temp-image.png';
// Image (logo) to be drawn
$logopath = 'content/images/qr/qr-freespace.png';
// we need to be sure ours script does not output anything!!!
// otherwise it will break up PNG binary!
ob_start("callback");
// text for the qr code
$codeText = 'Projectname'.$divider.$param;
// end of processing here
$debugLog = ob_get_contents();
ob_end_clean();
// create a QR code and save it in the filepath
QRcode::png($codeText, $filepath, QR_ECLEVEL_H, 9, 2, true );
// Start DRAWING LOGO IN QRCODE
$QR = imagecreatefrompng($filepath);
// START TO DRAW THE IMAGE ON THE QR CODE
$logo = imagecreatefromstring(file_get_contents($logopath));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
// Scale logo to fit in the QR Code
$logo_qr_width = $QR_width/3;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
imagecopyresampled($QR, $logo, $QR_width/3, $QR_height/3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
// Save QR code again, but with logo on it
imagepng($QR,$filepath);
// outputs image directly into browser, as PNG stream
readfile($filepath);
?>
There's a much better/modern library now available: https://github.com/chillerlan/php-qrcode
There is also an example for including space for a logo:
https://github.com/chillerlan/php-qrcode/blob/d650fe73067c2559519f09949e9b71b466ab2bee/examples/imageWithLogo.php

how to move watermark image to folder in php

i have created dynamic watermark image when user uploads using php, images are also displaying , but the problem is i don't know how to move the images into particular folder in my site
$path=JPATH_SITE.'/media/truematrimony/watermark-K'.$id.'_pho_2.jpg';
move_uploaded_file($_FILES["profile_multi2"]["tmp_name"],$path);
$font_path=$_SERVER['DOCUMENT_ROOT'].'/templates/srinivasmatrimony/font/GeosansLight.ttf';
$card = imagecreatefromjpeg($path);
$font_med = 12;
$white = imagecolorallocate($card, 0, 0, 0);
imagettftext($card, $font_lrg, , 40, , $white, $font_path,"kongumarriage.com");
$filenametemp= $_SERVER['DOCUMENT_ROOT'].'/media/truematrimony/watermark- K'.$id.'_pho_2.jpg';
imagejpeg($card, $filenametemp);
$ImageData = file_get_contents($filenametemp);
$ImageDataEnc = base64_encode($ImageData);
unlink($filenametemp);
Use the following function
move_uploaded_file ( string $filename , string $destination )
$filename - name of the file to move
$destination = folder where you want to move

php take image, rotate and save rotated image on server

Want to take image from own server rotate certain angle and save the image.
Image file $filename = 'kitten_rotated.jpg'; With echo '<img src='.$filename.'>'; i see the image.
Then
$original = imagecreatefromjpeg($filename);
$angle = 90.0;
$rotated = imagerotate($original, $angle, 0);
Based on this https://stackoverflow.com/a/3693075/2118559 answer trying create image file
$output = 'google.com.jpg';
If i save the same image with new file name, all works
file_put_contents( $output, file_get_contents($filename) );
But if i try to save rotated image, then file_put_contents(): supplied resource is not a valid stream resource.
file_put_contents( $output, $rotated );
Here https://stackoverflow.com/a/12185462/2118559 read $export is going to be a GD image handle. It is NOT something you can simply dump out to a file and expect to get a JPG or PNG image.. but can not understand how to use the code in that answer.
How to create image file from $rotated?
Tried to experiment, based on this http://php.net/manual/en/function.imagecreatefromstring.php
$fh = fopen( 'some_name.png' , 'w') or die("can't open file");
fwrite($fh, $data );
fclose($fh);
Does it means that need something like
$data = base64_encode($rotated);
And then write in new file?
I have not tested this, but I think you need to encode the image as base 64 first.
If you check the string from any Image URL, you'd see data:image/png;base64, preceding the hash. Prepending this to your image string and saving.
Here is a function that may help, based on what you already have:
// Function settings:
// 1) Original file
// 2) Angle to rotate
// 3) Output destination (false will output to browser)
function RotateJpg($filename = '',$angle = 0,$savename = false)
{
// Your original file
$original = imagecreatefromjpeg($filename);
// Rotate
$rotated = imagerotate($original, $angle, 0);
// If you have no destination, save to browser
if($savename == false) {
header('Content-Type: image/jpeg');
imagejpeg($rotated);
}
else
// Save to a directory with a new filename
imagejpeg($rotated,$savename);
// Standard destroy command
imagedestroy($rotated);
}
// Base image
$filename = 'http://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';
// Destination, including document root (you may have a defined root to use)
$saveto = $_SERVER['DOCUMENT_ROOT']."/images/test.jpg";
// Apply function
RotateJpg($filename,90,$saveto);
If you want to save image just use one of GD library functions: imagepng() or imagepng().
imagerotate() returns image resource so this is not something like string.
In your case just save rotate image:
imagejpg($rotated, $output);
And now You can use $output variable as your new filename to include in view like before:
echo '<img src='.$output.'>';
Don't forget to include appropriate permissions in directory where You're saveing image.

Resize the uploaded image using php

I need to resize the uploaded image and save it with given resolutions. Assume user uploads just only one single image and I save it like 35x35, 100x100 and 512x512 after finishing the upload. finally his one upload save in my folder as 3 images with different resolutions. I've done up to this point using laravel...
public function postSingleUpload()
{
//create the relevant directory to add the user image
//get the directory name (directory name equals to user id)
$dirPath = sprintf("images/users/avatar/%s/", Auth::user()->id);
//create the directory named by user id
if (!file_exists($dirPath)) {
mkdir($dirPath, 0700);
}
$file = Input::file('image');
//save image with given resulutions
//---- this part i need --------//
}
so please help me for this.
Here is what I have done to save the uploaded image with given resolutions:
//First Copy the uploaded image to some location
Input::file('profilePic')->move('Users/'.$username.'/Wallpics/',$name)
//Set this attribute for quality after resampling
$quality = 90;
$src = 'Users/'.$username.'/Wallpics/'.$name;
//Run this on recently saved uploaded image
$img = imagecreatefromjpeg($src);
//get this values from user by submitting form ( either by crop or by textboxes)
$width=(int)Input::get('w');
$height=(int)Input::get('h');
$x=(int)Input::get('x');
$y=(int)Input::get('y');
//This is the code to resample the image and generate a new to ur requirements
$dest = ImageCreateTrueColor($width, $height);
imagecopyresampled($dest, $img, 0, 0,$x,$y, $width, $height,$width,$height);
imagejpeg($dest, 'Users/'.$username.'/profilePic/'.$name, $quality);
//Set the path in database
$profile->profilePic=asset('Users/'.$username.'/profilePic/'.$name);
$profile->save();

Categories