dongtan1845 2011-09-13 17:35
浏览 65
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • dougeqiang1619 2011-09-13 17:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决