Saving base64 encoded image for facebook sharing via PHP - php

I have an image thumbnail, encoded as base64, that I want to use as the default thumbnail when sharing a page via Facebook. Facebook does not seem to support using the base64 image directly so I need to render/save/decode(?) it first before the user can click the "share" button. Any thoughts?
Here's my thumbnail:
$thumbnail = '<img src="data:image/jpg;base64,' . $thumbnail_src . '" />';
Obviously it renders fine in the browser but Facebook can't "get it."

Try looking at the GD Library first. And then create a php page that will render the image with the correct header so Facebook will see this as an image.

I ended up decoding the image and writing it to a temporary directory.

Related

Trying to display an echoed image from a PHP script in and `<img src="..."` tag

The objective:
1) I have a images folder outside web root where all images from the web app are placed (each image path is saved in a DB table);
2) I want to get that image upon a request from the user.
Simple, right?
I did:
a) Create a class to reads the image (with all security features and safeguards that I can remember). It returns (successfully) the image encoded (base64) with the respective mime type.
b) I've created, for demonstration purposes, a index.php, which has:
<img src="agivenscript.php?img=name.jpeg">
My only objective here is to call from any other place in my web app a script that outputs the base64 encoded image with the respective mime type.
c) Created a script "agivenscript.php" that receives the image name by GET, instantiates my class and gets, when all goes right, the base64 encoded image. It then echoes the image.
What happens:
When I do the output - using echo '<img src="' . $output64encodedwithmimetype . '">'; - in agivenscript.php it works like a charm;
Moreover, if I take the $output64encodedwithmimetype contents and place the string directly in the src tag of index.php, it also works.
However, it does not work when I try <img src="agivenscript.php?img=name.jpeg">.
As the base64 encoded image is obviously fine (and has the mime type), what am I missing? Any idea?
Thank in advance.
data: scheme URLs can be Base64 encoded.
HTTP responses of the image/jpeg (or png or whatever) can't. You need to serve up the actual image data without encoding it as text.
readfile is probably all you need.

Get the absolute url for BLOB image

I'm working with BLOB image in PHP. My images has no absulut url ( like this : www.example.com/my_image) . I need it because i'm woking with facebook open graph image sharing. How can i fix that?

Wrong width and height after upload to server

Source image
http://i.imgur.com/TbffELG.jpg
This is a vertical image, but when I upload it to server, server got the size width="3264" height="1836", and my resize and crop function will be wrong
there is the demo site http://demo.chan15.info/im/
PHP code
<?php
$file = $_FILES['file'];
$tmp = $file['tmp_name'];
$imageInfo = getimagesize($tmp);
echo '<pre>'; var_dump($imageInfo); echo '</pre>';
If the photo was taken on a mobile phone it can apply meta data regarding the orientation of the device at the time, which is used to infer the correct way to display the image. Not all decoders support the meta data, and will display the image incorrectly. In this case your image will probably display on its side.
You either need an image library capable of dealing with this meta data, or you can transform the image and/or remove the meta data. Apologies, but I can't suggest a suitable image library.
I had the same Problem. As HenryTK said the central problem is that the orientation of the picture is stored in the EXIF data (used by modern cameras and smartphones) while the width and length information indicate a landscape picture.
My solution is to open the picture with GIMP. GIMP shows the following modal dialog:
Now you only have to click the rotate button and save the picture.
It's not the best solution. If somebody has a script or something please tell me.

php delete image from server after upload & display

Sending an image in my php form, it gets saved onto my server and at the
action.php page it gets displayed. Now when I try to:
echo '<div id="image"><img src="'.$target_path.'" width="280" height="280"></div>';
it works just fine... but if I add unlink($target_path); at the end of my php code it
will not even display the image even though it gets deleted AFTER displaying the image...
So the question is, how can I display the image and deleting it at the same time so my server does not gets stuffed with user pictures?
Try another thing: output the image base-64 encoded.
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
echo '<div id="image"><img src="data:image/jpg;base64,'.$base64.'" width="280" height="280"></div>';
(instead of move_uploaded_file() etc, use as $file variable the $_FILES[...]['tmp_name'])
You can achieve this by creating a little script that gets the image-filename and will delete it after it has been retrieved:
<?php
$file = image_file_from_parameter($_GET['image']);
headers_for_file($file);
readfile($file);
unlink($file);
In your HTML output you then link to that script:
<img src="path/to/image.php?image=893sudfD983D" />
If you set nice caching headers, the user won't notice that your server did serve the file only once.
When you echo the url of an image with img src you're just sending the browser the url of an image, not the actual image data. The image needs to remain on the server if you want it to be viewable by this approach.
You could use bwoebi's solution to pass the actual image data instead of a link, but a better solution is just to keep the images on the server and periodically delete old files.

How do I directly display an image using PHP?

I have a question about displaying images in PHP. I need to have a PHP file display as JUST an image, as opposed to an image embedded in a web page, as if you had browsed to the JPEG image directly. The reason that I need it to be a PHP page as opposed to actually browsing to the image is that I need to resize the image before it is delivered. It would be easiest to use an image directly because that way I can display the image in a desktop application more easily. Is there any way to do this? Thanks!
<?php
header('Content-Type: image/jpeg');
readfile('path/to/image.jpeg');
You need to set the header Content-Type property to be the correct image type. See this link for the some examples.
http://www.electrictoolbox.com/image-headers-php/
Choose the correct header :
header("Content-type: image/png");
And use php gd to change the image size : http://php.net/manual/fr/book.image.php

Categories