使用php动态重采样热链接图像

使用php动态重采样热链接图像,php,watermark,image-resizing,hotlinking,Php,Watermark,Image Resizing,Hotlinking,我有一个图像目录,有些分辨率很高,所以如果它们被热链接,我想提供一个分辨率低得多的版本,并且我还覆盖了一个重复的图像(如水印),通知观众图像被盗,他们可以在哪里找到真正的原始图像 我已经让重采样工作良好,但当我添加了无热链接图像覆盖的功能,它不工作。问题是,我知道水印脚本本身也可以工作,因为我在网站的其他文件中使用过它。不幸的是,我不知道错误是什么,因为热链接测试站点不输出错误,它们只显示一个空的图像占位符,我的主机日志文件对我来说都是希腊语 这是我目前的剧本: <?php ini_set

我有一个图像目录,有些分辨率很高,所以如果它们被热链接,我想提供一个分辨率低得多的版本,并且我还覆盖了一个重复的图像(如水印),通知观众图像被盗,他们可以在哪里找到真正的原始图像

我已经让重采样工作良好,但当我添加了无热链接图像覆盖的功能,它不工作。问题是,我知道水印脚本本身也可以工作,因为我在网站的其他文件中使用过它。不幸的是,我不知道错误是什么,因为热链接测试站点不输出错误,它们只显示一个空的图像占位符,我的主机日志文件对我来说都是希腊语

这是我目前的剧本:

<?php
ini_set('memory_limit','250M');
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

class SimpleImage {

    var $image;
    var $image_type;

    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        if( $this->image_type == IMAGETYPE_JPEG ) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            $this->image = imagecreatefromgif($filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            $this->image = imagecreatefrompng($filename);
        }
    }
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=60, $permissions=null) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image,$filename,$compression);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image,$filename);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imagepng($this->image,$filename);
        }
        if( $permissions != null) {
            chmod($filename,$permissions);
        }
    }
    function output($image_type=IMAGETYPE_JPEG) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imagepng($this->image);
        }
        imagedestroy($image);
        exit();
    }
    function getWidth() {
        return imagesx($this->image);
    }
    function getHeight() {
        return imagesy($this->image);
    }
    function resizeToHeight($height) {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width,$height);
    }
    function resizeToWidth($width) {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width,$height);
    }
    function scale($scale) {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100;
        $this->resize($width,$height);
    }
    function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }
    function nothot() {
        $hotlink = imagecreatefrompng('hotlink.png');
        $hw = imagesx($hotlink);
        $hh = imagesy($hotlink);
        $img_paste_x = 0;
        $img_paste_x = 0;
        while($img_paste_x < $this->getWidth()){
            $img_paste_y = 0;
            while($img_paste_y < $this->getHeight()){
                imagecopy($image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
                $img_paste_y += $hh;
            }
            $img_paste_x += $hw;
        }
        imagedestroy($hotlink);
    }

}
header('Content-Type: image/jpeg');
$image = new SimpleImage();
$image->load($path);
$image->resizeToWidth(600);
$image->nothot();
$image->output();
?>

感谢您的帮助。

更改:

 imagecopy($image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
致:

另外,对于获取
$path
的方式也不确定,对图像是否存在的一些检查不会出错。

更改:

 imagecopy($image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);
致:


也不确定您获取
$path
的方式,一些关于图像是否存在的检查不会出错。

如果我完全不正确,我肯定会有人评论,但这会给您的服务器带来比仅提供图像本身更大的负载。也许最好存储一个较小的带有水印的版本,并将其提供给用户,而不是在每次请求时重复处理相同的巨大图像子集。甚至第一次存储它们,然后跳过重新成像。我同意这会容易得多,对服务器的压力也会小得多,但我的目标是在多用户站点上使用此脚本,因此如果我为每个用户的上传手动重新创建“热链接”版本,我就没有时间做其他事情了,所以我需要一个自动的方法来为我做到这一点。我相信如果我完全不正确,会有人评论,但这会给你的服务器带来更大的负载,而不仅仅是图像本身。也许最好存储一个较小的带有水印的版本,并将其提供给用户,而不是在每次请求时重复处理相同的巨大图像子集。甚至第一次存储它们,然后跳过重新成像。我同意这会容易得多,对服务器的压力也会小得多,但我的目标是在多用户站点上使用此脚本,因此如果我为每个用户的上传手动重新创建“热链接”版本,我就没有时间做其他事情了,所以我需要一个自动的方法来完成这件事。我想可能是这样简单的事情。我将研究路径验证问题,但如果映像不存在,则不会对我造成问题,仅针对hotlinker。谢谢我想可能是这么简单。我将研究路径验证问题,但如果映像不存在,则不会对我造成问题,仅针对hotlinker。谢谢