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
Related
I have written a function for creating a thumbnail from .jpg image. But what I want to do is whenever the function gets called, same image should be saved as 1.jpg,2.jpg,3.jpg at the same destination.
I have used Sessions and static variable concept but to no success.
Here is my code.
this is thumbsave.php file
<?php
session_start();
$_SESSION['sid']=$k=1;
function createThumb($fpath)//fpath will be passed here as parameter.
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims),
imagesy($ims));
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
$_SESSION['sid'] = $_SESSION['sid'] + 1;
imagedestroy($ims);
imagedestroy($imd);
echo "Thumbnail Created and Saved at the Destination";
}
?>
Here is my code for dynamicthumb.php
<?php
include("include/thumbsave.php");
createThumb("imgs/m1.jpeg");
?>
So,when I run the dynamicthumb.php file the image stored at the imgs folder must be stored inside saveimages folder. But here only 1 image is saved, not multiple copies are generated like 2.jpg,3.jpg.
You can use a file_exists loop if you are positive the name of the thumbnail will always be numerical:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = 1;
while ( file_exists('saveimages/'. $thumb .'.jpg') ) { $thumb++; }
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
However I question the logic behind what you are asking for... as this suggests that every thumbnail you create will just be a sequential number, all stored in the same directory?
You may have better luck using a counter db field or file, as doing file_exists on a folder with thousands and growing, can hurt performance.
So a file based counter solution could be thus:
function createThumb($fpath)
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);
imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));
$thumb = file_get_contents('saveimages/thumbcount.txt');
$thumb++; file_put_contents('saveimages/thumbcount.txt',$thumb);
imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
imagedestroy($ims);
imagedestroy($imd);
echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}
Just be sure to prime the thumbcount.txt with a 1 so it has something to begin with.
This is the line responsible for saving image to file:
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
You can update it to use $fpath instead of ,$_SESSION['sid']:
imagejpeg($imd, $fpath);
But beware, your path should end with .jpg.
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);
I want to create a image dynamically and upload it to a folder. I will be getting image for body,sleeve,collar and cuff dynamically from user selection. I am merging all these images to create a new image and this new image to uploaded in a folder. Am not able to upload the filename to folder and there is no errors displaying also but am able to generate the merged image.
Below is code,
<?php
$body = "img1.jpg";
$sleeve = "img2.jpg";
$collar = "img3.jpg";
$cuff = "img4.jpg";
$outputImage = imagecreatetruecolor(800, 800);
$background = imagecolorallocate($outputImage, 0, 0, 0);
imagecolortransparent($outputImage, $background);
$white = imagecolorallocate($outputImage, 255, 255, 255);
imagefill($outputImage, 0, 0, $white);
$first = imagecreatefrompng($body);
$second = imagecreatefrompng($sleeve);
$third = imagecreatefrompng($collar);
$fourth = imagecreatefrompng($cuff);
imagecopyresized($outputImage,$first,0,0,0,0,600,600,600,600);
imagecopyresized($outputImage,$second,0,0,0,0,600,600,600,600);
imagecopyresized($outputImage,$third,0,0,0,0, 600, 600, 600, 600);
imagecopyresized($outputImage,$fourth,0,0,0,0,600,600,600,600);
$filename = round(microtime(true)).'.png';
imagepng($outputImage, $filename);
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);
print_r(error_get_last());
if($ret)
{
echo "success";die;
}
else
{
echo "fail";die;
}
imagedestroy($outputImage);
?>echo "fail";die;
}
imagedestroy($outputImage);
?>
move_uploaded_file won't work on $filename because it's not an uploaded file - it was just created by your code.
Try switching the two lines (like below) and adding your path:
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
imagepng($outputImage, DIR_IMAGE . $filename);
And remove the move_uploaded_file line:
//$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);
you are using imagepng(), That saves file to particular location you are providing.
Syntax for imagepng() :
bool imagepng ( resource $image [, mixed $to [, int $quality [, int $filters ]]] )
where $to refers to location where you want to save your image (including filename).
So instead of
$filename = round(microtime(true)).'.png';
imagepng($outputImage, $filename);
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
$ret = move_uploaded_file($filename, DIR_IMAGE.$filename);
Try This,
$filename = round(microtime(true)).'.png';
define('DIR_IMAGE', '/opt/lampp/htdocs/dutees/image/design-uploads/');
imagepng($outputImage, DIR_IMAGE.$filename);
You don't need move_uploaded_file() anymore, because the image created is already in your server.
I also had similar problem while creating captcha for new users, and This solution worked.
You should try file_put_contents instead of move_uploaded_file.
file_put_contents('you_file_location/filename', IMAGE_OBJECT_HERE);
or you can also save file using fwrite function.
I have problem with my reszie image (code) when I run this code in localhost the code is work fine, but when I implement in website. the code give a warning.
<br /><b>Warning</b> : imagecreatefromjpeg([-1, [], 17, 1, 18, 1, 19, 23, true, [true, true],26,1,27,1,30,1,33]) expects parameter 1 to be resource, boolean given in <b>fungsi/f_upload_banner.php</b> on line <b>34</b><br />
This is my code for resize
<?php
$target_dir = "../uploads/images/banner/";
$image1 =$_FILES['txtfile']['name'];
$filename1 = stripslashes($_FILES['txtfile']['name']);
$ext1 = substr($image1, strrpos($image1, '.')+1);
$idimg1 = md5(uniqid() . time() . $filename1) . "-1." . $ext1;
$target_file1 = $target_dir . basename($idimg1);
//identify images file
$realImages = imagecreatefromjpeg($target_file1);
$width = imageSX($realImages);
$height = imageSY($realImages);
//save for thumbs size
$thumbWidth = 150;
$thumbHeight = ($thumbWidth / $width) * $height;
//change images size
$thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImage, $realImages, 0,0,0,0, $thumbWidth, $thumbHeight, $width, $height);
//save thumbnail images
imagejpeg($thumbImage,$target_dir."thumb_".$idimg1);
//remove images object from memory
imagedestroy($realImages);
imagedestroy($thumbImage);
?>
Where is wrong?
You're trying to use the original file name (after mangling) as the source for imagecreatefromjpeg(), when you should be using the temporary name assigned by the upload process: $_FILES['txtfile']['tmp_name']
Do this:
$realImages = imagecreatefromjpeg($_FILES['txtfile']['tmp_name']);
You're also not moving the uploaded file to a permanent location. The temporary version will be deleted when your script terminates and the file will be lost.
Note also that your code is doing no error checking whatsoever, so if your upload fails you won't know about it. See the PHP section on Handling File Upload
I applied a filter to the .jpg images of a folder as follows:
$images = glob('images/*.jpg');
foreach($images as $image) {
$image_prefix = "image-beautiful-";
$image = imagecreatefromjpeg($image);
$rand = rand(50, 500);
imagefilter($image, IMG_FILTER_COLORIZE, 75, 15, 10);
$new_image = imagejpeg($image, $image_prefix .$rand.'.jpg');
imagedestroy($image);
}
The generated images save in the main directory. How can I directly save them to another folder like generated-images using PHP?
according to the doco for imagejpg, the second parameter is the path and filename, so try adding the folder to the image prefix:
$image_prefix = "generated-images" . DIRECTORY_SEPARATOR . "image-beautiful-"