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

发布于 2024-12-04 04:23:00 字数 3563 浏览 0 评论 0原文

我有一个图像目录,其中一些具有很大的分辨率,因此如果它们被热链接,我想提供分辨率低得多的版本,并且我还覆盖了重复的图像(如水印),通知查看者图像被盗,并且在那里他们可以找到真正的原件。

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

这是我目前的脚本:

<?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();
?>

感谢您的帮助。

I have an images directory, some with massive resolutions, so I'd like to serve a much lower resolution version if they get hotlinked and I'm also overlaying a repeating image (like a watermark) informing the viewer that the image is stolen and where they can find the genuine original.

I've got the resampling working fine but when I add the function for the no hotlink image overlay, it doesn't work. The thing is, I know the watermarking script works too, on it's own, because I've used it elsewhere in another file on the site. Unfortunately I can't tell what the error is as the hotlink test sites don't output errors, they just show an empty image placeholder, and my host's log files are all like greek to me.

This is the script I have at the moment:

<?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();
?>

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-12-11 04:23:00

更改

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

为:

 imagecopy($this->image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);

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

Change:

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

to:

 imagecopy($this->image, $hotlink, $img_paste_x, $img_paste_y, 0, 0, $hw, $hh);

Also not sure about the way you get the $path, some checks on whether the image exists wouldn't go a miss.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文