duangu6431 2012-09-25 22:02
浏览 38
已采纳

PHP ImageMagick图像的多种尺寸 - 无法使其工作

Hi all : ) Thank you in advance for any assistance with this question.

I am using CodeIgniter and have images uploading, rotating and resizing successfully. It is when I try to create multiple sizes fro an image that I am a little lost.

Here is my code:

// First I make two copies of the origional image - there is no problem here - these always work. I always have the successfully copied images.

                $Copy_Mid_Size_File = "Some_Path_For_The_Newly_Copied_Mid_Size_Image"

                if(!copy($source_image, $Copy_Mid_Size_File)){
                    echo "failed to copy $Copy_Mid_Size_File - Please contact the system administrator. 
";
                    die();
                }

                $Copy_Thumbnail_Size_File = "Some_Path_For_The_Newly_Copied_Thumbnail_Size_Image"

                if(!copy($source_image, $Copy_Thumbnail_Size_File)){
                    echo "failed to copy $Copy_Thumbnail_Size_File - Please contact the system administrator. 
";
                    die();
                }

                // Now I resize for the mid size image
                $config['image_library'] = 'imagemagick';           
                $config['library_path'] = '/usr/bin';

                $config['source_image'] = $Copy_Mid_Size_File;
                $config['maintain_ratio'] = TRUE;
                $config['quality'] = '100%';
                $config['master_dim'] = 'auto';
                $config['width'] = 200;
                $config['height'] = 200;

                $this->load->library('image_lib', $config);

                if(!$this->image_lib->resize()){
                    echo $this->image_lib->display_errors();// This has never reported an error yet
                    echo "Image Re-size for Mid Size Image FAILED!";// This has never reported an error yet
                    die();
                }else{
                    echo" Mid-Size Re-size Worked!";
                }


                // Now I try to resize for the Thumbnail
                $config['image_library'] = 'imagemagick';           
                $config['library_path'] = '/usr/bin';

                $config['source_image'] = $Copy_Thumbnail_Size_File;

                $config['maintain_ratio'] = TRUE;
                $config['quality'] = '100%';
                $config['master_dim'] = 'auto';
                $config['width'] = 50;
                $config['height'] = 50;

                $this->load->library('image_lib', $config);

                if(!$this->image_lib->resize()){
                    echo $this->image_lib->display_errors();// This has never reported an error yet
                    echo "Image Re-size for Thumbnail Size Image FAILED!";// This has never reported an error yet
                    die();
                }else{
                    echo" Thumbnail Re-size Worked!";
                }

I always end up with the proper number of images correctly named - The thumbnail image just does not re-size - there is no error reported. It always says that is succeeded.

If I put the thumbnail re-size code first - the thumbnail re-sizes correctly - but then the mid-size image does not.

I realize there are other ways to load the libraries - but I don't see why the first re-size works but the second does not.

Any ideas?

Thanks. Regards, Ken

  • 写回答

1条回答 默认 最新

  • dongyu9894 2012-09-26 17:03
    关注

    You only need to load the library once, but then you need to initialize it with the new config each time. So your code will look like this:

    // load the library
    $this->load->library('image_lib');
    
    // first image:
    // set some config
    $config['image_library'] = 'imagemagick';
    ...
    
    // initialize the library with this config
    $this->image_lib->initialize($config);
    // Do the resize:
    $this->image_lib->resize();
    
    // clear the config:
    $this->image_lib->clear();
    
    // second image:
    // set some config
    $config['image_library'] = 'imagemagick';
    ...
    
    // re-initialize the library with the new config
    $this->image_lib->initialize($config);
    // Do the resize:
    $this->image_lib->resize();
    

    I suspect you could even just update $config['width'] and $config['height'] without having to clear, but I've not tested that!

    As a side note, you don't really have to copy the image into the correct directories first; the CodeIgniter image library can create a new one for you, if you set your config like this:

    $config['new_image'] = '/path/to/new_image.jpg';
    

    This will save the image into the new path without having to create copies all over the place.

    Reference: http://codeigniter.com/user_guide/libraries/image_lib.html

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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么