I want to add force download functionality with the help of
$this->load->helper('download');
$photo_path = "uploads/default/photos/".$photo;
$name = $photo_name.'.jpg';
$data = file_get_contents($photo_path); // Read the file's contents
$name = $photo_name.'.jpg';
force_download($name, $data);
Now i want to add watermark image over the image before downloading the image.is this possible with image manipulation library or should i try to add watermark when uploading files.
Here's how you can add the watermark before sending it to force_download:
// Get the watermark from a file
$watermark = imagecreatefrompng('watermark.png');
$wmsize = getimagesize('watermark.png');
// Get your source image
$image = imagecreatefromjpeg($photo_path);
$size = getimagesize($photo_path);
// Set the watermark to be centered within the size of the destination image
$dest_x = ($size[0] - $wmsize[0]) / 2;
$dest_y = ($size[1] - $wmsize[1]) / 2;
// Copy the watermark over the original image
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $wmsize[0], $wmsize[1]);
// Use output buffering to capture the output to send to force_download
ob_start(); //Stdout --> buffer
imagejpeg($image);
$img2 = ob_get_contents(); //store stdout in $img2
ob_end_clean(); //clear buffer
imagedestroy($image);
force_download($name, $img2);
Related
I have a script that upload an image file to rackspace container, what I want to happen after that is
Create a copy of that image file.
Put the file into memory and resize the image.
Upload the file to the rackspace container with new file name.
Trouble is I'm not sure how to take the resampled image in memory and upload to rackspace container. Here is code.
$objectStoreService = $client->objectStoreService(null, 'IAD');
$container = $objectStoreService->getContainer(IMAGE_CONTAINER_NAME);
$fileData = fopen($_FILES['file-0']['tmp_name'], 'r');
$uniqid = uniqid();
$new_file_path = $uniqid."/".$_FILES['file-0']['name'];
$sm_file_path = $uniqid . "/" . 'sm_' .$_FILES['file-0']['name'];
// Upload filepath to database
$user->upload_profile_pic($new_file_path, $sm_file_path);
$result = $container->uploadObject($new_file_path, $fileData);
$new_file_path = IMAGE_CONTAINER_LINK."/".$new_file_path;
// Make Thumbnail of original image
$percent = 0.5; // percentage of resize
// Image to be changed.
$original_image = imagecreatefromjpeg($new_file_path);
list($width, $height) = getimagesize($new_file_path);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Temporary blank image in memory of height/width
$tmp_image = imagecreatetruecolor($new_width, $new_height);
// Copies the source image and copies into it the blank image and resamples it.
imagecopyresampled($tmp_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Save the image in memory
imagejpeg($tmp_image, NULL, 100);
// read the file
// Not sure how to put image in memory to rackspace cloud.
$fileData = fopen($tmp_image, 'r');
$tmp_image = imagecreatefromstring(file_get_contents($new_file_path));
$fileData = fopen($tmp_image, 'r');
$result = $container->uploadObject($sm_file_path, $fileData);
$response = array('image_path' => $new_file_path);
header('HTTP/1.1 201 Created');
echo json_encode($response);
I am trying to rotate image using php imagerotate function but its not working.
GD Library is also on.
i have tried this ,
public function rotate()
{
$targ_w = 240;
$targ_h = 180;
$jpeg_quality = 100;
$degrees = 90;
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
$rotatedImage = imagerotate($image,$degrees,0);
imagejpeg( $rotatedImage,$src,$jpeg_quality);
imagedestroy($rotatedImage);
die();
}
<?php
// File and rotation
$filename = 'test.jpg';
$degrees = 180;
// Content type
header('Content-type: image/jpeg');
// Load
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, $degrees, 0);
// Output
imagejpeg($rotate);
// Free the memory
imagedestroy($source);
imagedestroy($rotate);
?>
You're outputing the unchanged $image to file. You should output the rotated one.
imagejpeg( $rotatedImage,$name ,$jpeg_quality);
The second thing - your image is empty. It has only defined width and height but has no content inside it. You defined a $src variable but you don't use it at all.
Maybe you want to replace imagecreatetruecolor with this:
$src = "/photos/sunset.jpg";
$image = imagecreatefromjpeg($src);
You must output the rotated image (pass $rotatedImage instead of $image):
$rotatedImage = imagerotate($image,$degrees,0);
header('Content-type: image/jpeg'); //Header is required to output the image.
imagejpeg($rotatedImage,$name ,$jpeg_quality);
imagedestroy($rotatedImage);
die();
If you are trying to show the image then you need to change that:
header('Content-type: image/jpeg'); //Add jpeg header
imagejpeg( $rotatedImage, NULL, 100); //<-- Notice i remove the $src parameter
If you want to update your jpg file then your code will work, but the user that runs the php file need permissions to write the file. Of course your current image will be overwritten.
And as i said in comments you will need GD version 1.8 or later to work with jpeg files according to php.net
I am passing values from the img src like this:
PageA:
<img src="watermark.php?fname=<?php echo $image; ?>" />
then I ge the value on pageB:
$fname = $_GET['fname'];
$watermark_img = $passed_fname;
...
For some reason no image is showing ... I'm I missing something here?
Here is pageB full code:
<?php
$fname = $_GET['fname'];
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
//$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$watermark_img = $fname;
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Does $image contains some special characters?
Try to use PHP urlencode() function:
<img src="watermark.php?fname=<?php echo urlencode($image); ?>/>
And the, on the second page:
$fname = urldecode($_REQUEST['fname']);
If the value you pass on $fname is just the name, you might need to specify the full path.
$watermark_img = '/PATH_TO_THE_IMAGE/'.$fname;
Try the watermark PHP directly from the address bar, then view source. If there is any output before you set the jpeg header, the browser will be unable to deal with it. You might have something like
Error line 3443234
JPEGIMAGECODE
That first line makes it an invalid image.
I am using an image watermark code that works great but I also need to add a text watermark to it.
Here is the code in full:
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 100; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
You can use imagestring() there are other image text function so checkout the manual for GD
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
I'd like to show the image in this page using an <img> tag instead of an header ( header("content-type: image/jpeg"); ). any ideas?
<?php
$main_img = "Porsche_911_996_Carrera_4S.jpg"; // main big photo / picture
$watermark_img = "watermark.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 50; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
This script generates an image, its output is an image, so you can use it in a tag, but in an other page:
<img src="image.php" /> <!-- (if image.php is the name of the script you posted) -->
<img src="yourscript.php"/>
or have your script output the image in some file and then use this file.
any ideas?
Don't do it. It is theoretically possible to embed images in HTML using data: URI's, but it has too many downsides and is usually a bad idea. The classical approach using an <img> tag that references the PHP script is the way to go.
you work with the ob_start() and ob_get_contents()
if($_GET['image'])
{
ob_start();
$main_img = "a.jpg"; // main big photo / picture
$watermark_img = "b.gif"; // use GIF or PNG, JPEG has no tranparency support
$padding = 3; // distance to border in pixels for watermark image
$opacity = 50; // image opacity for transparent watermark
$watermark = imagecreatefromgif($watermark_img); // create watermark
$image = imagecreatefromjpeg($main_img); // create main graphic
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
$watermark_size = getimagesize($watermark_img);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($main_img);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
// copy watermark on main image
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
$s = ob_get_contents();
ob_clean();
echo $s;
}
?>
[img src="echo '?image=1'?>"]
at the end you can in the src of image tage do this
echo '?image=1'