Combine two images (.JPG) using PHP GD - php

I can't find a solution to this. I want to add 20px empty space to this image:
http://img233.imageshack.us/img233/419/78317401.jpg
and then paste this watermark at the bottom (on the blank space)
So the output would be:
http://img252.imageshack.us/img252/4554/wynik.jpg
I don't want to stretch it.
EDIT
Did it with WIdeImage. Rly simple.

1) Load both images with
http://www.php.net/manual/en/function.imagecreatefromjpeg.php
2) Get 1st image height and width with
http://www.php.net/manual/en/function.imagesy.php
http://www.php.net/manual/en/function.imagesx.php
3) Create larger image with height+20
http://www.php.net/manual/en/function.imagecreatetruecolor.php
4) Copy the first image and the second image to the larger image
http://www.php.net/manual/en/function.imagecopy.php
5) Save it
http://www.php.net/manual/en/function.imagejpeg.php
6) Done

Try wide image api http://wideimage.sourceforge.net/
Check out one of their this demo may be that help you
Merge and
Resize

Related

Full image instead of thumbnail

We have a shop page on this website.
I will explain my problem using the very first image in the top left as example, although it applies to all of them.
If you hover over the first image (Gentes Deluxified English) you see an incomplete picture like this:
src= ".../Gentes-board-DLX-20180123-200x267.jpg"
This image actually cuts of parts of the images to the left and right. The full image is like this:
src= " .../Gentes-board-DLX-20180123.jpg"
So we want to change the thumbnail image to the full image, or keep the original ratio of the image. This needs to work for all images and the ones added in the future.
Is this fixable with css?
I already spent a lot of time looking at the source code of woocommerce to find the file that creates the page that uses the thumbnail images. I can't find that file. Where should I look for the source code files? And what should I change in these source files?
I looked at the source code of Woo commerce and found the parameter for cropping thumbnails for you.
https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-regenerate-images.php
Add this
$crop = false;
after line 318:
private static function get_image( $fullsizepath, $thumbnail_width, $thumbnail_height, $crop ) {

How to make image's background transparency to 0?

There are images displayed inside a HTML table in a PHP code :
The code of this table is :
for ($i = 0; $i < $data['list']['cnt']; $i++) {
$tabRows[$i][1]['width'] = "45%";
$tabRows[$i][1]['align'] = "center";
$tabRows[$i][1]['value'] = '<img src="'.HTTP_FIGURES.$data['list'][$i]['fig_image'].'" />';
}
As you can see the background of the images are seen and they make the page dirty. So I want to remove the background of each image. How to do that ?
Make the images a transparent PNG.
Your only practical solution is to go through each image with an image editor, delete the background and re-save as a .png with transparency.
Take any image editor like GIMP (free), Paint.net(free), Photoshop or any other image editor and add transparency where you need it. Here is a tutorial for paint.net. If your images are not in PNG - you will need to make PNG images as JPEG has no transparency.
You have 2 options:
Edit images with a image editor like paint.net and make the backgrounds transparent.
Use the PHP GD functions to 'edit' the images. This can be cumbersome, because you have to determine what color you want to replace with a background color. Often, a fixed color is used, or the color in pixel[1,1].
My advise, if there are not to many images, go for 1.

Watermark an image upon download...is it possible?

Simple question with yes/no answer and if yes...then how?
Suppose you have an HTML page with an image on it without any sort of watermark.
Is it possible to place a watermark on that image if a user saves it to their computer?
I need a simple function that watermarks an image upon download or save...
I do understand that once the image loads in the browser, it is technically downloaded, so is there a way to display the image without a watermark on screen, and if the user opens browser cache, he/she finds a watermarked copy?
If anybody has done this using any platform (PHP, GD, jQuery, etc.), your contribution would be appreciated.
One trick might be to combine 2 images, so they become one.
I have image A:
Then I add image B (watermark version)
So when you display the image for the user you use one as background and the other one as image, so when user tries to download, they will get only one part. Of course as already mentioned, the user will be able to get all they can see on the screen, but most users won't be able to combine the images properly.
Please note that the image on top must be transparent.
I would recommend doing this server side and cache the modified images when you have cut out the watermark
Here you can read how to do it with PHP: http://www.sitepoint.com/watermark-images-php/
I personally don't think that it is possible with javascript, because as you already have said yourself, it is already downloaded.
But don't nail me on that.
On the server side it is for sure possible, as you can see in the above link.
A possible alternate solution is to contain the image inside an element with a hidden overflow.
For example:
Your image has a height of 200px, you add an extra 20px watermark (when uploading) at the bottom of the image (so it isn't actually on top of the image). So the total image now has a height of 220px; but you place it inside an element give that element a 200px height and a hidden overflow.
You can change the source of the image when a user right-clicks it. This way you can change the source to the watermarked version when the user tries to save the image.
Yes, the user will already have the non-watermarked version in their cache, but only advanced users are going to know how to get to those images.
$('img').on('mousedown', function (event) {
//check which mouse button was clicked, 1 == left, 2 == middle, 3 == right
if (event.which == 3) {
//update the source of this image
this.src = this.src.replace('.jpg', '_watermarked.jpg');
}
});​
Here is a demo: http://jsfiddle.net/s6A9m/
Anything that the user can see they can take. There is no way to watermark ONLY if downloaded. When an image is displayed in the browser it has already downloaded.
There are several approaches you could take. I would recommend you use PHP to add the watermark to the image before it is displayed. This means that all protected images on the site will display a visible watermark. A second approach I have seen used is to display a low quality version that is not watermarked, but restrict the full quality version to only those who are supposed to see it.
Yes it is possible. All you have to do is,
Call the download function, in which we can implement the downloading of watermarked images.
I have used AngularJS, HTML, JQuery and watermarkJS which you find here(http://brianium.github.io/watermarkjs/)
HTML:
Download Sample
Javascript:
$scope.selectedWatermarkType = 'Text';
$scope.selectedPosition = 'Bottom-left';
$scope.text = 'Sample Watermark';
$scope.size = '45';
$scope.selectedFont = 'Arial';
$scope.ColorCodeGlobalObj.colorcode = 'ffff';
$scope.DownloadSample = function () {
if ($scope.selectedWatermarkType == 'Text') {
if ($scope.selectedPosition == 'Bottom-left') {
watermark(['/assets/js/node_modules/watermarkjs/examples/img/shepherd.jpg'])
.image(watermark.text.lowerRight($scope.text, $scope.size + 'px ' + $scope.selectedFont, '#' + $scope.ColorCodeGlobalObj.colorcode, 0.5))
.then(function (img) {
saveImageAs(img);
});
}
}
'img' is the watermarked image object which can be used to download the image.
function saveImageAs(imgOrURL) {
imgOrURL.src.replace('image/png', 'image/octet-stream');
if (typeof imgOrURL == 'object')
imgOrURL = imgOrURL.src;
var link = document.createElement("a");
link.setAttribute("href", imgOrURL);
link.setAttribute("download", 'watermarkSample.png');
link.click();
}
This code performs download of watermarked image. Still the unwatermarked image is available in the mentioned srcpath which can be appended to an 'img' tag and can be viewed.
Hope this helps !

Print stylish text on image in php

Is it possible to print html div tag with style on image in PHP?. if not, then what is the alternative way?
Some hosts have ImageMagick for PHP. To add text to your image, take a look at the syntax of the commands here. The example given on that page should help some - it's pretty easy to get text on an image.
The benefits of using ImageMagick over a fixed image is that you can vary the content of the text, which is what you might want (you didn't mention needing a static text; for this, I'd use an image with a transparent background). For more comprehensive font commands, take a look here.
To put a transparent image on top of your base image, take a look at this very nicely designed site.
I'll also give the code presented on that site here:
$photo = imagecreatefromjpeg("original.jpg");
$watermark = imagecreatefrompng("watermark.png");
// This is the key. Without ImageAlphaBlending on, the PNG won't render correctly.
imagealphablending($photo, true);
// Copy the watermark onto the master, $offset px from the bottom right corner.
$offset = 10;
imagecopy($photo, $watermark, imagesx($photo) - imagesx($watermark) - $offset, imagesy($photo) - imagesy($watermark) - $offset, 0, 0, imagesx($watermark), imagesy($watermark));
// Output to the browser
header("Content-Type: image/jpeg");
imagejpeg($photo);
To output the image to a file, please Google that and replace the last two lines of the example given above.
For ImageMagick stuff, take a look here
I hope this helps :-)
James
You can set the image as the background graphic of any div using CSS. Then the text within that div will appear on top of the image.
(CSS)
.mydiv {
background:url(/path/to/image.gif);
width:100px; /* set to width of the image */
height:100px; /* set to height of the image */
}
(HTML)
<div class='mydiv'>Some text here</div>
There is no easy way to print text on images using html/css on server side, because php can't parse html, so you'd better find another solution like php GD.

Is it possible to change image position and width with php GD?

I have a gif file with many icons and buttons on it..
What I want to do is include that gif file using php gd somefunction("file.gif")
then, resize the new image to 30px by 30px.. and then be able to position (using x and y coordinates) the actual gif file, so that only a certain area of the image shows on the new file..
just like the css background-position property but, with gd.
Thanx
imagecopyresized() resizes the image, so it doesn't quite work for what I was exactly looking for.
For anyone else that might need this, the best function is actually imagecopy().
http://www.php.net/manual/en/function.imagecopy.php
It will do the trick..
Thanx Trufa for the quick answer though.
imagecopyresized()
http://www.php.net/manual/en/function.imagecopyresized.php
Should do the trick for adjusting the size.

Categories