Php 在我自己的域中加载远程映像而不复制
我想在我的域中加载一个外部映像,而不在我的服务器上复制该映像,就好像我正在托管此映像一样。Php 在我自己的域中加载远程映像而不复制,php,Php,我想在我的域中加载一个外部映像,而不在我的服务器上复制该映像,就好像我正在托管此映像一样。 我尝试过通过curl将其复制到我的服务器。但我要它不要复制。i、 e.外部图像位置为 现在,我想加载此映像,而不复制服务器中的文件。有没有关于如何在php中实现这一点的建议?在我的工作场所,我们使用这样的方法。注意:它更大,因为图像的大小调整为适合150x150平方的化身。只需删除该部分并使用width,$height而不是$newwidth即可获得原始文件。我想自己把它取下来,但现在是圣诞节的早晨,我只
我尝试过通过curl将其复制到我的服务器。但我要它不要复制。i、 e.外部图像位置为
现在,我想加载此映像,而不复制服务器中的文件。有没有关于如何在php中实现这一点的建议?在我的工作场所,我们使用这样的方法。注意:它更大,因为图像的大小调整为适合150x150平方的化身。只需删除该部分并使用
width
,$height
而不是$newwidth
即可获得原始文件。我想自己把它取下来,但现在是圣诞节的早晨,我只是没心情
.htaccess
.photo.php
如果您的图像有特定的目录结构,例如:它们都存在于/public/images下,那么您可以编辑您的服务器设置(httpd.conf或apache2.conf,具体取决于您的服务器),并添加类似于以下内容的代理:
ProxyPass /public/images http://examplemydomain.com/public/images
记住在完成更改后重新启动apache/httpdSimple asnwer-这很复杂。复杂答案-使用一些php脚本作为图像的src
,该脚本将在远程服务器上获取图像文件的内容并将其输出。让我们先试试复杂的答案。)这一点也不复杂,一条简单的.htaccess线路可以直观地将任何远程主机映像/url重定向到您的主机,但首先,这将如何影响原始主机的版权?嘿,谢谢您的回答。但是我不需要自定义图像。我通过一个简单的头函数实现了这一点。但我真的很想知道如何仅通过使用.htaccess来实现这一点,正如你所说的。
<?php
// Images (not the safest way to check though )
$allowed_ext = array(
'gif' => 'image/gif',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg'
);
$square_size = 150;
// Get file data
$file_name = trim($_GET['fn']);
$file_path = 'http://example.com/' . urlencode($file_name);
list($width, $height) = @getimagesize($file_path);
// Do something ONLY if file is found
if (!empty($width) && !empty($height)) {
$ext = pathinfo($file_path, PATHINFO_EXTENSION);
// We accept only predefined images. if not image extension - DIE!
if (empty($allowed_ext[$ext])) {
die();
} else {
$mtype = $allowed_ext[$ext]; // Set our predefined meme types ¯\_(ツ)_/¯
}
// Get new image ratio
$ratio = ($width > $height) ? $square_size / $width : $square_size / $height;
// Resize only if original is larger then allowed
if ($ratio < 1) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($file_path);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
header('Content-Type: ' . $mtype);
imagejpeg($image_p, null, 100);
}
die;
ProxyPass /public/images http://examplemydomain.com/public/images