The problem with hot linking to an image is that the image can change or the link may no longer be valid. In a worst case scenario, images can change to something unpleasant at the source and this change will be reflected where ever it's being hot linked.
For this reason it's better to have local copies of images and serve it from your own server. Naturally you will still need to administer the image to determine if it complies with your website rules, but once it has been approved you can be sure it wont change.
Most social networking sites download images from links found in user posts and serve the local copies when viewing the images. A draw back of this approach is that you will need adequate server space to store the images and a great deal of bandwidth. Y
Downloading images in PHP is relatively an easy task proving the setting allow_url_fopen is set to true in php.ini. The process involves first determining if the user posted content contains any URLs. This can be achieved using the preg_match_all function with a regular expression to find all URLs in the content. The URLs can then be looped over and the data found at the URL can be downloaded. But first the URL must be evaluted to determine if it is in fact an image. Once downloaded, you then have the choice on how you want to present it the the user.
Implementation
PHP provides a number of functions to match strings found in a subject string. You can use the preg_match_all function to find all URLs in a post.- $content = "This is user posted content with a URL http://www.yourdomain.com/the-image.jpg image
- followed by another http://www.yourdomain.com/thes-second-image.jpg";
- preg_match_all('%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s', $content, $matches);
The variable $maches in the above code will contain all matched URLs at index position 0, so you can loop through the elements at this position to retrieve the the URLs. The next step is to determine if the URL is an image. This can be done by issuing a simple request to the URL and examining the response headers. One of the headers should be a Content Type header, which will indicate if the URL is an image. The server hosting the image must be configured correctly so that the correct Content Type is returned. It is possible for a server to return a different Content Type therefore this method is not always reliable. For the purpose of this article we will examine the Content Type by first issuing a request.
- foreach($matches[0] as $url){
- if($fp){
- }
- }
- }
The $meta variable in the above code will contain a key/value array if the request was successful. Inspecting the array will reveal an array element wrapper_data, which will contain an array of all the response headers. The code below demonstrates how the Content Type can be determined by performing a series of string manipulations.
- <?php
- $content = "This is user posted content with a URL http://www.yourdomain.com/the-image.jpg image
- followed by another http://www.yourdomain.com/thes-second-image.jpg";
- preg_match_all('%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s', $content, $matches);
- foreach($matches[0] as $url){
- if($fp){
- $headers = $meta['wrapper_data'];
- // Loop through the headers
- foreach($headers as $headerAndValue){
- // Check if the header contains the string Content-Type
- // Spilt the string to get the header and value
- // Check if the value contains the string image
- // Download file here
- }
- }
- }
- }
- }
- }
- }
- ?>
PHP provides a number of ways to determine Content-Type that can greatly simplify the code above. One such function is the stream_context_create function that is used in conjunction with the stream_get_meta_data function. Once you familiarize your self with the process you can optimize the code providing better error handling and by making use of the mentioned stream* functions
Finally, you must decide on how you want to show the images. The final code sample below shows how URLs are matched and replaced with the img HTML tag where the source of the image points to the local downloaded file.
- <?php
- $content = "This is user posted content with a URL http://www.yourdomain.com/the-image.jpg image
- followed by another http://www.yourdomain.com/thes-second-image.jpg";
- preg_match_all('%\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))%s', $content, $matches);
- foreach($matches[0] as $url){
- if($fp){
- $headers = $meta['wrapper_data'];
- // Loop through the headers
- foreach($headers as $headerAndValue){
- // Check if the header contains the string Content-Type
- // Spilt the string to get the header and value
- // Check if the value contains the string image
- // Download the image file
- // Split the url to get the image extension
- // Create a new filename for the image
- // Save the file
- // Replace the URls with an image tag
- }
- }
- }
- }
- }
- }
- }
- echo $content;
- ?>
No comments:
Post a Comment