首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

搞了两天,没有弄成,宣布失败,求高手帮忙!

2013-07-08 
搞了两天,没弄成,宣布失败,求高手帮忙!!下面这个代码,是可以直接把图片处理为四个角都为园的。但是现在产生

搞了两天,没弄成,宣布失败,求高手帮忙!!
下面这个代码,是可以直接把图片处理为四个角都为园的。
但是现在产生的图片是没生成文件的,想改为处理的文件可以生成一个在指定目录。。
弄了两天,总是不行。。。求高手帮忙看看。。谢谢!
例如运行网址:http://www.xxx.com/pic.php?gopic=1234.jpg

pic.php代码如下:


<?php  
class RoundedCorner {  
    private $_r;  
    private $_g;  
    private $_b;  
    private $_image_path;  
    private $_radius;  
      
   function __construct($image_path, $radius, $r = 255, $g = 255, $b = 255) {   
        $this->_image_path = $image_path;  
        $this->_radius = $radius;  
        $this->_r = (int)$r;  
        $this->_g = (int)$g;  
        $this->_b = (int)$b;  
    }  
      
    private function _get_lt_rounder_corner() {  
        $radius = $this->_radius;  
        $img = imagecreatetruecolor($radius, $radius);  
        $bgcolor = imagecolorallocate($img, $this->_r, $this->_g, $this->_b);  
        $fgcolor = imagecolorallocate($img, 0, 0, 0);  
        imagefill($img, 0, 0, $bgcolor);  
        imagefilledarc($img, $radius, $radius, $radius*2, $radius*2, 180, 270, $fgcolor, IMG_ARC_PIE);  
        imagecolortransparent($img, $fgcolor);  
        return $img;  
    }  
      
    private function _load_source_image() {  
        $ext = substr($this->_image_path, strrpos($this->_image_path, '.'));  
        if (empty($ext)) {  
            return false;     


        }  
        switch(strtolower($ext)) {  
            case '.jpg':  
                $img = @imagecreatefromjpeg($this->_image_path);  
                break;  
            case '.gif':  
                $img = @imagecreatefromgif($this->_image_path);  
                break;  
            case '.png':  
                $img = @imagecreatefrompng($this->_image_path);  
                break;  
            default:  
                return false;  
        }  
        return $img;  
          
    }  
      
    public function round_it() {  
        // load the source image  
        $src_image = $this->_load_source_image();  
        if ($src_image === false) {  
            die('错误:图片不存在');   
        }  
        $image_width = imagesx($src_image);  
        $image_height = imagesy($src_image);  
          
        // create a new image, with src_width, src_height, and fill it with transparent color  
        $image = imagecreatetruecolor($image_width, $image_height);  


        $trans_color = imagecolorallocate($image, $this->_r, $this->_g, $this->_b);  
        imagefill($image, 0, 0, $trans_color);  
          
        // then overwirte the source image to the new created image  
        imagecopymerge($image, $src_image, 0, 0, 0, 0, $image_width, $image_height, 100);  
          
        // then just copy all the rounded corner images to the 4 corners  
        $radius = $this->_radius;  
        // lt  
        $lt_corner = $this->_get_lt_rounder_corner();  
        imagecopymerge($image, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);  
        // lb  
        $lb_corner = imagerotate($lt_corner, 90, $trans_color);  
        imagecopymerge($image, $lb_corner, 0, $image_height - $radius, 0, 0, $radius, $radius, 100);  
        // rb  
        $rb_corner = imagerotate($lt_corner, 180, $trans_color);  
        imagecopymerge($image, $rb_corner, $image_width - $radius, $image_height - $radius, 0, 0, $radius, $radius, 100);  
        // rt  
        $rt_corner = imagerotate($lt_corner, 270, $trans_color);  
        imagecopymerge($image, $rt_corner, $image_width - $radius, 0, 0, 0, $radius, $radius, 100);  
          
        // set the transparency  
        imagecolortransparent($image, $trans_color);  


        // display it  
        header('Content-Type: image/png');  
        imagepng($image);  
          
        imagedestroy($src_image);  
        imagedestroy($image);  
        imagedestroy($lt_corner);  
        imagedestroy($lb_corner);  
        imagedestroy($rb_corner);  
        imagedestroy($rt_corner);  
    }   

}  
$rounder = new RoundedCorner($_GET['gopic'], 20);  


$rounder->round_it();  
?>


[解决办法]
本帖最后由 xuzuning 于 2013-06-23 19:07:04 编辑 public function round_it($filename='') {
  ....
        header('Content-Type: image/png');  
        if($filename) imagepng($image, $filename);  
        else imagepng($image);
        imagedestroy($src_image);  
        imagedestroy($image);  
        imagedestroy($lt_corner);  
        imagedestroy($lb_corner);  
        imagedestroy($rb_corner);  
        imagedestroy($rt_corner);  
}    
[解决办法]
引用:
public function round_it($filename='') {
  ....


        header('Content-Type: image/png');  
        if($filename) imagepng($image, $filename);  
        else imagepng($image);
        imagedestroy($src_image);  
        imagedestroy($image);  
        imagedestroy($lt_corner);  
        imagedestroy($lb_corner);  
        imagedestroy($rb_corner);  
        imagedestroy($rt_corner);  
}    



使用版主的修改,然后调用的时候
$rounder->round_it(你要保存的目录);  
[解决办法]
上面的错了,是应该改成把
imagepng($image);  
改为
imagepng($image,$this->_image_path);
看错了
[解决办法]
哦,还有注意一点的是,因为你这程序还没有复制移动(即上传)到别的文件夹的功能,所以,你所生成的圆角图片覆盖的是原图片

热点排行