Ajax image grabber - php

I need an image grabber. By that I mean a Digg like image grabber that can search other pages (including youtube, normal websites, economist,...whatever), get the images that are of decent sizes and if I select it, I can upload it to my server.
Does anyone know of a plugin for this?
Thanks.

I don't know about any off-the-shelf library. But I once needed a quick way to retrieve the "main image" off a page. My best guess was to just get the largest in file size. I was using the PHP SimpleHTMLDom library for easy access to the site's <img> tags.
Now, here's the main part of the code, that returns the URL of the biggest image file for a given page.
Hope you can build on that.
// Load the remote document
$html = file_get_html($url);
$largest_file_size=0;
$largest_file_url='';
// Go through all images of that page
foreach($html->find('img') as $element){
// Helper function to make absolute URLs from relative
$img_url=$this->InternetCombineUrl($url,$element->src);
// Try to get image file size info from header:
$header=array_change_key_case(get_headers($img_url, 1));
// Only continue if "200 OK" directly or after first redirect:
if($header[0]=='HTTP/1.1 200 OK' || #$header[1]=='HTTP/1.1 200 OK'){
if(!empty($header['content-length'])){
// If we were redirected, the second entry is the one.
// See http://us3.php.net/manual/en/function.filesize.php#84130
if(!empty($header['content-length'][1])){
$header['content-length']=$header['content-length'][1];
}
if($header['content-length']>$largest_file_size){
$largest_file_size=$header['content-length'];
$largest_file_url=$img_url;
}
}else{
// If no content-length-header is sent, we need to download the image to check the size
$tmp_filename=sha1($img_url);
$content = file_get_contents($img_url);
$handle = fopen(TMP.$tmp_filename, "w");
fwrite($handle, $content);
fclose($handle);
$filesize=filesize(TMP.$tmp_filename);
if($filesize>$largest_file_size){
$largest_file_size=$filesize;
$largest_file_url=$img_url;
unlink(TMP.$tmp_filename);
}
}
}
}
return $largest_file_url;

Related

Whether there generate QR code in my project directory will get mix when multi people using it?

I have a question about the application generate QR code image.
I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it.
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
my code is bellow:
function generate_qrcode($url){
$filename = 'hante_qrcode.png';
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
//generate QR code image
$o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<pre>";
print_r($o);
print_r('<img src="hante_qrcode.png">');
}
if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
yes
how to solve this problem?
there are two ways to solve this problem
you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples
don't store image on server and find some js to generate qr code directly from client side.
having a one demo for that check if you can use it
var qrcode = new QRCode("qrcode");
qrcode.makeCode('https://stackoverflow.com');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<div id="qrcode"></div>
Of course it will be overwritten.
Solution 1
Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url.
$filename = md5($url) . ".png";
if(!file_exists($filename)){
$o = QRcode::png($url, $filename, ...);
}
echo '<img src="'.$filename.'">';
Solution 2
If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html.
// image.php
// Still we want to give uniqe filename because we can get another request while one request is processing
$filename = md5(microtime) . "_qr.png";
$o = QRcode::png($url, $filename, ...);
$image = file_get_contents($filename);
// remove the file after stored in a variable
unlink($filename);
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
echo $image;
// index.html
<img src="image.php?url=someurl">

Glass Mirror API save attatchment image to server via PHP

I am using the PHP quick start project example to display the timeline's attachment (image):
<?php
if ($timeline_item->getAttachments() != null) {
$attachments = $timeline_item->getAttachments();
foreach ($attachments as $attachment) { ?>
<img src="<?php echo $base_url .
'/attachment-proxy.php?timeline_item_id=' .
$timeline_item->getId() . '&attachment_id=' .
$attachment->getId() ?>" />
<?php
}
}
?>
Now I need to save the image to the server so I can resize it and use it elsewhere.
I have tried a few variations of file_put_contents, fopen, and curl but it seems attachment-proxy.php is not returning the image in a format that any of these expect.
How can save a Timeline Attachment to my server?
SOLUTION: Based on Prisoner's response I took another look at the attachment-proxy.php file. It is returning the image as a string. I had unsuccessfully tried file_put_contents($img, file_get_contents("attachment-proxy.php....")); before.
Turns out I don't need the file_get_contents() part.
I altered the last few lines of attachment-proxy.php to this:
$img = $_GET['timeline_item_id'].'.jpg';
$image = download_attachment($_GET['timeline_item_id'], $attachment);
file_put_contents($img, $image);
It works. It saves the image to my server with the ID as the file name.
Thanks.
Have you checked to see what it is returning? The attachment_proxy.php requires OAuth to have been completed, and will redirect you through the OAuth flow if this hasn't been done. So it may very well be that it is saving the HTML for the OAuth login page, or the information from the redirect page.
However, if you're trying to setup something on your server that calls your own server's attachment_proxy.php page... you're jumping through additional unnecessary hoops.
You can probably take a look directly at attachment_proxy.php to see how it is getting the attachment data from Google's servers, and then use this same method to get them and store them on your server instead of just feeding it out for the img tag. Looking at https://github.com/googleglass/mirror-quickstart-php/blob/master/attachment-proxy.php it seems like most of the work is done in a call to download_attachments() which is located in https://github.com/googleglass/mirror-quickstart-php/blob/master/mirror-client.php. You should be able to either borrow the code from download_attachments() or call it directly yourself.

Playing mp4 files from rar or zip files

I have one problem. I would like to order from (zip) files play video in such a way to get out connection so you can include it in HTML5 badge. As I gave an example. But this not working.
<?php $video = fopen('zip://video.zip#video.mp4', 'r'); ?>
<video>
<source src='<? echo $video; ?>' type='video/mp4' />
</video>
$video in the above code is just a server-side file handle you could use to read the file from the zip. It's not directly usable for the browser.
You'll need to handle reading the file and returning it in a separate HTTP request. Usually you'd use a second script for this. (Or if your video is relatively small, you might be able to use data urls, but it's not something I'd try to do.) Additionally, if you want to allow for byte range requests, you'd have to handle that yourself in your video serving logic.
Here's a fairly simple scenario:
My videos.zip file contains a couple of different videos, and I want to retrieve a specific one and show it on a page called video.php
First I have my viewer, say video.php, [edit: containing the video tag and with a URL to my second script as the source. Since I might want to serve the other file at some point, I set it up to accept a filename in the v query parameter.]
..some html/php..
<video>
<source src='zipserve.php?v=itsrainintoast.mp4' type='video/mp4' />
</video>
..more html/php..
Then, in zipserve.php I have something like this:
$filename = $_GET['v']; //You probably want to check that this exists first, btw.
$fp = fopen('zip://videos.zip#'.$filename, 'r');
if($fp)
{
header('content-type: video/mp4');
//Note: you should probably also output an appropriate content-length header.
while(!feof($fp))
{
echo fread($fp, 8196);
}
fclose($fp);
}
else
{
echo 'Some error message here.';
}
--Addendum--
It should also be noted that this'll require the zip php extension to be enabled.
A more complete example of a video fetching script with range handling and the like can be found in the question at mp4 from PHP - Not playing in HTML5 Video tag but you'd need to tweak it to allow reading from the zip file.

Fetching an image from another server

Say, for this example, this is an image: http://www.basitansari.com/wp-content/uploads/logoaba.png which is hosted on website A.
I am calling this image into website B with an <img> tag. If the owner of site A removes this image then how can I know that the image has been removed using PHP? If the owner of site A removes the image then I want to show a notfound.jpg image. Should I use cURL for this purpose, or something else?
Checking the image server-side is definitely possible, but it may not be the best solution. Doing so means that all valid images will be loaded twice (once server-side and again by the client). A better method may be to use JavaScript to handle broken images. The following code will only take action if an error occurs while loading the image, replacing it with the image you specify:
<script type="text/javascript">
function brokenImage( obj ) {
obj.onerror = '';
obj.src = 'path/to/notfound.jpg';
return true;
}
</script>
<img onerror="brokenImage(this);" src="broken.jpg" alt="" />
if (!$fp = fopen('http://www.basitansari.com/wp-content/uploads/logoaba.png', 'r')) {
// The image cannot be retrieved, handle it here.
} else {
fclose($fp);
}
This is the quickest way as it just checks that the image can be successfully retrieved, it doesn't download the whole file.
You should call that URL with HEAD method and check if status code 404 (Not found) or 200 (OK)
You can call directly: Check if image source exists using fopen and if not show notfound.jpg. # is used to supress warning
<img src="<?php if (#fopen("http://www.basitansari.com/wp-content/uploads/logoaba.png", "r")) {echo 'http://www.basitansari.com/wp-content/uploads/logoaba.png';} else{echo 'notfound.jpg';}?>" />

Downloading a photo from twitpic url

Is there any way of downloading an image from Twitpic URL? Let's say I want to get next photo http://twitpic.com/49275c.
The corresponding link for an image with ID of 49275c is given by
http://twitpic.com/show/full/49275c for a full sized image.
Replace 'full' with 'thumb' or 'mini' for different sizes.
http://twitpic.com/show/[size]/[image-id]
You should really have a look at the API and tinker:
http://dev.twitpic.com/
As #Gordon has pointed out in a comment, twitpic seems to have an API -- which you should use, for this kind of thing.
See : http://dev.twitpic.com/
Now, here's the old answer -- fun, but not really a good idea, considering there is an API :
The URL you have is not the URL of the image itself : it's the URL of an HTML page, in which the image is displayed, in an <img> tag.
So, you need to act in two steps :
First, load that page, and extract the URL of the <img> tag.
This can probably be done using DOMDocument::loadHTMLFile
Then, when you have the image's URL, you can download it
Using file_get_contents,
Or curl.
And here's an example of code that does that :
$twitpic_url = 'http://twitpic.com/49275c';
$dom = new DOMDocument();
if (#$dom->loadHTMLFile($twitpic_url)) {
// HTML loaded successfully
// => You need to find the right <img> tag
// Looking at the HTML, you'll see it has id="photo-display"
$img_tag = $dom->getElementById('photo-display');
$src = $img_tag->getAttribute('src');
// Just to be sure, let's display the image's URL
var_dump($src);
// Now, you have to download the image which URL is $src
$img_content = file_get_contents($src);
// ANd do whatever you want with that binary image content
// like save if to a file :
file_put_contents('/tmp/my-image.jpg', $img_content);
}
Note : you have to add some checks here and there -- like check if the photo-display element exists, for instance.
You can do it directly from the Twitter API, using the entities parameter.

Categories