【发布时间】:2012-01-26 09:14:21
【问题描述】:
我正在努力让 CodeIgniter Image Manipulation 正常工作。要么它是一个错误,要么我只是没有看到它。我希望有人可以帮助我。提前致谢!
关于脚本:我想创建缩略图 (176w x 132h)。输入图像具有不同的大小和比例。为了始终获得这个尺寸,我首先调整它们的大小以适应最大宽度或高度(取决于图像方向),然后在中心裁剪。 我试图用一种方法来完成这一切。这不起作用,所以我创建了两个单独的方法。
resize_img()
和
crop_img().
当我在 3 个不同的文件上运行 resize_img() 时,它可以工作。如果在那之后我在这些缩略图上使用crop_img() 创建的第一个方法,它就可以工作。如果我将两者结合起来,或者一个接一个地使用它们,它不会。 我试过 $this->image_lib->clear();,取消设置配置文件。为了确定,我什至创建了两个配置文件。
我从 GD2 得到不同的各种错误,但问题是,在 resize_img() 创建缩略图之后,crop_img() 不会裁剪它。之后,一切都向南,下一个图像无法打开。检查文件夹和文件的写入权限。
无法保存图像。请确保图像和文件 目录是可写的。图像的路径不正确。您的 服务器不支持处理此类型所需的 GD 函数 图像。
完整代码:
<?PHP
class Imagetest extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
function index()
{
$testimg1 = 'uploads/test/1.png';
$testimg2 = 'uploads/test/2.png';
$testimg3 = 'uploads/test/3.png';
$this->resize_img($testimg1);
$this->crop_img($testimg1);
$this->resize_img($testimg2);
$this->crop_img($testimg2);
$this->resize_img($testimg3);
$this->crop_img($testimg3);
}
function image_thumb_name($img = null)
{
if(!empty($img)) {
$exploded = explode('.', $img);
return $exploded['0'] . '_thumb.' . $exploded['1'];
}
}
function get_axis($img)
{
// Default values
$output['x_axis'] = 0;
$output['y_axis'] = 0;
// Settings
$config['height'] = 132;
$config['width'] = 176;
if ($img_dim = getimagesize($img)) {
list($thumb_width, $thumb_height) = $img_dim;
} else {
echo '<h1> ERROR HERE TOO</h1>';
return false;
}
if ($thumb_width > $config['width']) {
$output['x_axis'] = (($thumb_width - $config['width']) / 2) ;
} else if ($thumb_height > $config['height']) {
$output['y_axis'] = (($thumb_height - $config['height']) / 2);
}
return $output;
}
function resize_img($img)
{
$config = array();
echo 'Processing: '. $img .'<br/>'; // Debug
if ($img_dim = getimagesize($img)) {
list($image_width, $image_height) = $img_dim;
} else {
echo '<h1> ERROR HERE </h1>';
}
// create a thumbnail
$config['image_library'] = 'GD2';
$config['source_image'] = $img;
$config['quality'] = 100;
$config['height'] = 132;
$config['width'] = 176;
$config['create_thumb'] = TRUE;
$config['maintain_ratio']= TRUE;
$config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width';
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config);
}
function crop_img($img)
{
$config2 = array();
// Crop that thumbnail
$config2['image_library'] = 'GD2';
$config2['quality'] = 100;
$config2['height'] = 132;
$config2['width'] = 176;
$config2['source_image'] = $this->image_thumb_name($img);
$axis = $this->get_axis($config2['source_image']);
$config2['x_axis'] = $axis['x_axis'];
$config2['y_axis'] = $axis['y_axis'];
$config2['maintain_ratio'] = FALSE;
$config2['create_thumb'] = FALSE;
$this->image_lib->initialize($config2);
if (!$this->image_lib->crop()) {
echo $this->image_lib->display_errors();
}
echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug
$this->image_lib->clear();
unset($config2);
}
}
【问题讨论】:
-
我仍然无法正常工作。有什么想法吗?
标签: php image-manipulation codeigniter-2