0

I have two domains sharing the same server. One of them, site_1.com, holding images at directory /images and the other one site, site_2.com, must to show those images. I have the following code at site_2:

<html>
<body>
<img src="http://site_1.com/images/img.png" />
</body>
</html>

Result: It didn't work. After reading a lot of answers and comment here and other forums I applied their sugges of using for example an api at site_1 like this:

/api/get_image.php at site_1.com:

<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
header('Content-type: image/png');
imagepng(imagecreatefrompng($url));
?>

And call the api at site_2.com like this:

<html>
<body>
<img src='http://site_1.com/api/get_image.php?file_name='img.png' />
</body>
</html>

It didn't work. Another way using file_get_contens():

<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
$img = file_get_contents($url) ;
echo $img ;
?>

It didn't work. Another way using CURL library:

<?php
$file_name = $_GET['file_name'];
$url = 'http://site_1.com/images/'.$file_name;
//
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
echo $image ; 
?>

It didn't work.

It's problably that I'm doing something wrong but I don't know what is. By the way, allow_url_fopen is ON and may be I have to active some other flag in some configuration file of apache or php. Please, could anybody tell me where is the problem? I appreciate any help.

héctor
  • 3
  • 3
  • 1
    has nothing to do with it being on the other domain. you have html code inside of a php block. – chiliNUT Feb 15 '14 at 18:57
  • 1
    there is no need for php to show cross domain images, you can do it in html – chiliNUT Feb 15 '14 at 18:58
  • Just take out the HTML code from the PHP brackets. – Akshat Singhal Feb 15 '14 at 18:59
  • thanks a lot chiliNUT for your fast answer. the html code inside php is my fault trying to illustrating what I did. Could you please be more explicit? I'm a completely newbie in web programming. – héctor Feb 15 '14 at 19:09
  • "It didn't work." is never a good description of a problem. *What* didn't work? Did you see an error? Did your browser not display the image? Does going to the image URL directly work? Are you trying to load images from a site which might be deliberately detecting such loading to prevent abuse of bandwidth and/or copyright? – IMSoP Feb 15 '14 at 19:43
  • 'it didn't work' means web browser not show the image, there are not any error message in the log file of the site. Only firebug say 'aborted' at status column of the GET img.png. I am not trying to load images from any where. The sites are on my own server and I'm programming and testing my own sites. I'm working on these from some time but in my notebook with windows and wamp installed and both sites work perfectly, the problem came when a pass to a new server with ubuntu server 12.04 lts. – héctor Feb 15 '14 at 20:00

1 Answers1

1

sharing the same server.

Put images to some shared folder (i.e. both users for site_1 and site_2 can have access to it) and create symlinks to public folders of both sites

If you want to use your solution I think you should send appropriate header, e.g. header('Content-type: image/jpg');

header('Content-Type: image/'.$fileType);
readfile($filePath);
ishenkoyv
  • 665
  • 4
  • 9
  • Thanks very much ishenkoyv for your answer. Finally I found the problem. When I pass the sites to the new server I put in c:/windows/.../etc/hosts file the neccessary line to resolve www.site_2.com but I forgot the line to resolve www.site_1.com that is the holder of the images. I appreciate ours attention. – héctor Feb 16 '14 at 20:30