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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程