下面这个代码,是可以直接把图片处理为四个角都为园的。
但是现在产生的图片是没生成文件的,想改为处理的文件可以生成一个在指定目录。。
弄了两天,总是不行求高手帮忙看看。。谢谢!
例如运行网址:http://www.xxx.com/pic.php?gopic=1234.jpgpic.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();
?>
但是现在产生的图片是没生成文件的,想改为处理的文件可以生成一个在指定目录。。
弄了两天,总是不行求高手帮忙看看。。谢谢!
例如运行网址:http://www.xxx.com/pic.php?gopic=1234.jpgpic.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();
?>
使用版主的修改,然后调用的时候
$rounder->round_it(你要保存的目录);
imagepng($image);
改为
imagepng($image,$src_image);
试试
imagepng($image);
改为
imagepng($image,$this->_image_path);
看错了
imagepng($image);
改为
imagepng($image,$this->_image_path);
就好了
其他的都不要变
使用版主的修改,然后调用的时候
$rounder->round_it(你要保存的目录); 也不行呐、、搞得我都乱了。。求直接贴出代码啊。。谢谢啊!
你还是直接把代码贴出来吧。。我难道还忽悠你不好吗?真的弄了用不行
贴出来吧,然后结贴了。。我直接把 imagepng($image); 改为 imagepng($image,$this->_image_path); 了其他的都没改
你自己试了吗???????
你自己试了吗???????
无语,我不试怎么又那2张图片?
无法说直接 www.xxx.com/pic.php?pic=123.jpg 这样通过文件执行来生成一个文件到指定目录?
无法说直接 www.xxx.com/pic.php?pic=123.jpg 这样通过文件执行来生成一个文件到指定目录?晕死,当然可以啊文件复制移动,怎么就不可以呢?
继续求救如何写?
继续求救如何写?
我想问你,你懂文件复制移动么?简单的不能再简单只要在
private function _load_source_image() {
这个函数的
$ext = substr($this->_image_path, strrpos($this->_image_path, '.'));
if (empty($ext)) {
return false;
}
这段代码下面加一个复制移动文件的代码就OK,即:
if (file_exists($this->_image_path) === TRUE) {
if(!file_exists('okpic')){
@mkdir('okpic');
}
$newpath='okpic/'.$this->_image_path;
if (!copy($this->_image_path, $newpath)){
return false;
}
$this->_image_path=$newpath;
}这么简单的复制移动而已
$newpath='okpic/'.$this->_image_path;
改成
$newpath='okpic/'.date('YmdHis').$ext;
这样就可以重命名并且复制移动了
现在有个问题,就是 http://www.xxx.com/pic.php?gopic=uploadfiles/123.jpg
这样的话,加了uploadfiles的路径目录,总是提示图片不存在,但是却会生成了一个文件到okpic里面?
代码哪里出问题呢?