如何从文件获取内容调整图像大小


How to resize image from file get content

我正在使用代码点火器,我想调整通过 url 上传的用户的图像大小。

这是我的代码:

            //upload via url
            $url = $this->input->post('photo');
            /* Extract the filename */
            $filename = substr($url, strrpos($url, '/') + 1);
            /* Save file wherever you want */
            file_put_contents('myuploads/'.$filename, file_get_contents($url));
            //resize start
            $config['image_library'] = 'gd2';
            $config['source_image']  = $filename;
            $config['overwrite']     = TRUE;
            $config['width'] = 59;
            $config['height'] = 90;
            $this->load->library('image_lib', $config);
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $data=array(
                'username'=>$this->input->post('username'),
                'deskrip'=>$this->input->post('deskrip'),
                'photo'=>$filename
              );
            $this->db->where('id',$id);
            $outp = $this->db->update('user',$data);

上传正在工作,但问题是图像不会调整为 59x90 并且仍然保持原始大小。

有什么答案吗?

非常感谢..

resize() 方法是否有效,您可以通过以下方式看到错误:

    if ( ! $this->image_lib->resize())
{
    echo $this->image_lib->display_errors();
}

请参阅文档有一个很好的图像操作教程,来自tutsplus

希望这对你有帮助。

谢谢

可能

有点偏离主题(不是通过codeigniter),但我发现这种方法在我的项目中更容易一些。

我使用此逻辑为图片执行裁剪逻辑。

$value = "picture.jpg";
$x = $p[0];
$y = $p[1];
$w = $p[2];
$h = $p[3];
$targ_w = $w;
$targ_h = $h;
$jpeg_quality = 90;
$src = REAL_PATH."uploads/".$value;
$src_end = $path."/".$value;
$ext = pathinfo($src, PATHINFO_EXTENSION);
if(($ext == "jpg" || $ext == "jpeg") && isset($x) && isset($y) && isset($w))
{
    $img_r = imagecreatefromjpeg($src);
    $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
    imagecopyresampled($dst_r,$img_r,0,0,$x,$y,
    $targ_w,$targ_h,$w,$h);
    unlink($src);
    imagejpeg($dst_r,$src_end,$jpeg_quality);
}