Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Image Library
  • I'm currently working on an image library for FuelPHP, since I havn't seen any out there yet. It works at the moment, but needs some work to follow the standards of FuelPHP. Currently it supports GD2 and ImageMagick for resizing, cropping, rotation, and mirroring (flip/flop). These are the current manipulation functions: Github: https://github.com/dudeami/fuel Function List:
    /**
     * Loads the image and checks if its compatable.
     * 
     * @param string $filename The file to load
     * @return Image_Driver
     */
    public static function load($filename)
    
    /**
     * Crops the image using coordinates or percentages.
     * 
     * Absolute integer or percentages accepted for all 4.
     *  
     * @param integer $x1 X-Coordinate based from the top-left corner.
     * @param integer $y1 Y-Coordinate based from the top-left corner.
     * @param integer $x2 X-Coordinate based from the bottom-right corner.
     * @param integer $y2 Y-Coordinate based from the bottom-right corner.
     * @return Image_Driver
     */
    public static function crop($x1, $y1, $x2, $y2)
    
    /**
     * Resizes the image. If the width or height is null, it will resize retaining the original aspect ratio.
     * 
     * @param integer $width The new width of the image.
     * @param integer $height The new height of the image.
     * @param boolean $keepar Defaults to true. If false, allows resizing without keeping AR.
     * @return Image_Driver
     */
    public static function resize($width, $height, $keepar = true)
    
    /**
     * Rotates the image
     * 
     * @param integer $degrees The degrees to rotate, negatives integers allowed.
     * @return Image_Driver
     */
    public static function rotate($degrees)
    
    /**
     * Adds a watermark to the image.
     * 
     * @param string $filename The filename of the watermark file to use.
     * @param string $position The position of the watermark, ex: "bottom right", "center center", "top left"
     * @param
     * @return Image_Driver
     */
    public static function watermark($filename, $position, $padding = 5)
    
    /**
     * Adds a border to the image.
     *
     * @param integer $size The side of the border, in pixels.
     * @param string $color A hexidecimal color.
     * @param Image_Driver
     */
    public static function border($size, $color = null) {
    
    /**
     * Masks the image using the alpha channel of the image input.
     *
     * @param string $maskimage The location of the image to use as the mask
     * @return Image_Driver
     */
    public static function mask($maskimage) {
    
    /**
     * Adds rounded corners to the image.
     *
     * @param integer $radius
     * @param integer $sides Accepts any combination of "tl tr bl br" seperated by spaces, or null for all sides
     * @param integer $antialias Sets the antialias range.
     * @return Image_Driver
     */
    public static function rounded($radius, $sides = null, $antialias = null)
    
    /**
     * Saves the image, and optionally attempts to set permissions
     * 
     * @param string $filename The location where to save the image.
     * @param string $permissions Allows unix style permissions
     */
    public static function save($filename, $permissions = null)
    
    /**
     * Outputs the file directly to the user.
     * 
     * @param string $filetype The extension type to use. Ex: png, jpg, bmp, gif
     */
    public static function output($filetype)
    

    Any suggestions on image functions that would be nice? I plan on doing watermarking and thumbnails, but any more are welcome. I will throw what I have up on github once I finish it up a bit more.
  • First off, sorry I haven't been around as much as I'd like. Got swamped with a few jobs, and was working roughly every minute I was awake, give or take a few minutes. For the bugs out their, I have not looked into them yet, but will shortly. For the gradient map function, I like what I see, but a few things that could be smoothed out. For the function that actually does the processing, making it a single call to the convert method would be best. I have plans to make it only use one call to the exec, but that's when I get enough time to wrap my head around it. Anyways, if you can sum it up in one command, the would be the best solution, but if you can't it'll work as-is. For the Image_Driver::_gradient_map(), nothings wrong but it is never called. The reason that exists is to do as much as possible with the input variables for all drivers, then do driver-specific things in the that driver's function. Right now its fine, just letting you know you never use that function in the Image_Imagemagick driver. That is just what ran through my mind, and right now it looks fine to me. If possible, a Imagick version would be nice.
  • Hello, dudeami! Will be there support of imagick driver?
    I tested imagick and image magick and first was faster from php;)
  • Ok, my laptop is back in action ;D I should have something up in about a day or two. Sadly, I fell behind in my college courses and those take priority. Anyways, right now I am making these base functions: - Resize
    - Crop
    - Rotate/Mirror
    - Border
    - Convert (from one type to another type)
    - Watermarking
    - Thumbnail Creation
    - Output
    - Save These will be the basic ones I will get up and going, then I'll try and add the rest of the functions from the image moo library and possibly do the masking. For these basic functions I will try and support both GD and imagemagick, but I will try and make the imagemagick driver support more of its features.
  • Good to hear you are ok :) When you are done, I'm willing to test it out and report problems if found :)
    Le'ts make it the best image lib over the net :)
  • @Rostislav Mind showing the image-related code? It might have something to do with the clear_queue or persistence config options. By default it will not clear a previously done action (in otherwords, the queue).
  • I was looking for a similar to CI's image lib clear method for clearing but couldn't find it in the docs or in the source of image class. Now that you mentioned it I looked harder for clear_queue and found it in the config. After setting it to true everything's fine. Sorry to bother you.
  • No problem :p That caused a bit of confusion for a few, maybe it should default to clear_queue being true?
  • I had plans for it, I just need to work on it in linux (As all the work I've done is on a Windows 7 64bit, which means no Imagick). I do have a linux server at home all setup, so we'll see. I'm also going to try and speed up the imagemagick library by using one call the the convert(.exe) instead of several.
  • Yes, maybe the general case is you need to clear the queue after a save. It's probably a rare case to be the opposite. :)
    Thanks for the great lib btw it saved me a lot of time, especially the presets. I've previously done some extending of CI's img lib to automate stuff like this and having it in the core image class is grrreat :)
  • Is there problems with GD transparency for png's or gif's now? If yes, I can help to fix them. I've patched CI image lib for transparency issues in my last project.
  • Everything but image_rotate supports transparency. If you can get that working, that would be awesome :D All other features support transparency.
  • Ok, so I came up with an idea the other day for preset commands. In the config file, you define presets like:
    /**
     * These presets allow you to call controlled manipulations.
     */
    'presets' => array(
    
        /**
         * This shows an example of how to add preset manipulations
         * to an image.
         *
         * Note that config values here override the current configuration.
         *
         * Drivers cannot be changed in here.
         */
        'example' => array(
            'quality' => 100,
            'bgcolor' => '#FFFFFF',
            'imagetype' => 'png',
            'actions' => array(
                array('crop', '10%'),
                array('resize', 150, 150, true),
                array('rotate', -90),
                /**
                 * Variables passed to the preset function (such as $this->preset('example', '/www/public/images/image.png) )
                 * can be used to set variables in the presets. In this function, the $1 would be replaced by
                 * '/www/public/images/image.png'
                 */
                array('save', '$1')
            )
        )
    )
    

    Then in the code, you can call:
    Image::load('/path/to/file.ext')
    Image::preset('example', '/path/to/newfile.ext);
    

    Right now how it works is: - Get the config for this preset
    - Override any settings that exist in the preset
    - Perform all actions
    - Revert the config to its original status Any ideas on that? I think it'd be useful when generating thumbnails and doing repetitive work. Right now its coded, along with most of the main driver and GD driver. I'll be sure to upload it to my github once its ready for testing.
  • Kris K wrote on Saturday 4th of June 2011:
    I'll leave a message here when I send it, then you can just take it from the repo or wait until its merged (They are usually quick with that)

    Have you had time to investigate the problem?
  • hi guys! I'm getting this error message below after download the latest from the develop repository. It was working fine before. Fuel\Core\Fuel_Exception [ Error ]: imagemagick executables not found in C:/wamp/imagemagick/ Any idea why?
  • A Huzz wrote on Sunday 3rd of July 2011:
    hi guys! I'm getting this error message below after download the latest from the develop repository. It was working fine before. Fuel\Core\Fuel_Exception [ Error ]: imagemagick executables not found in C:/wamp/imagemagick/ Any idea why?
    You'll have to create a config file in your app/config folder for image.php and change the Imagemagick executable location from that WAMP location to wherever you have it installed. I can't comprehend why the C:/ WAMP directory is the core defined configuration setting for Fuel, but it is. I would try to figure out how to access Imagemagick executables before giving up in favor of GD. Imagemagick is faster. OH, and to answer your actual question. Prior to recently, all functions were being passed through to the GD driver, regardless of configuration. So an error like this one wouldn't have shown, as GD was being used. If GD suits you well, all you have to do is follow the directions on this page: http://fuelphp.com/docs/classes/image.html#configuration
  • Hello. For me it looks really nice. good thoughts!
    Having configs saved somewhere could save a lot of time - good idea Also don't forget about resize_crop() :)
    We also need a method like save_pa() for this to work:
    'actions' => array(
                // if we not change quality, keep the one prom config (current 100)
                array('resize_crop', 150, 150),
                array('save_pa', 'prepend_text_', '_thumb_150x150_100') // here it would create the new file. 100 in the append is the quality value (it's usefull when you wanna check if specific files exists, if no, regenerate the thumbnail from original)
                array('quality', 90), // we change the quality for further processing
                array('resize_crop', 500, 500), // working with original file
                array('save_pa', '', '_thumb_500x500_90') // ofc prepend and append text would be hardcoded or passed to the config somehow ?
                array('resize_crop', 500, 500), // here we would still work with the original file
                array('save', '$1') // here it would overwrite the original file
            )
    

    One more function I think would be nice, is smth like blur. For example I would show a user 'blured' photo untill he logs in, etc. what do you think dudeami? for now these are all thought on image lib Good work dudeami!
  • The reason it defaults to being in a WAMP folder is because I develop on windows, and forget to change it to '/usr/bin' most of the time. Really the Imagick driver should be used if you can install the extension, since that is faster yet. Since Imagick is not available for every OS (Windows doesn't have it, don't think Mac does either), the ImageMagick driver is a good alternative for people like me who develop on Windows and deploy on *nix.
  • Rostislav, we had some time with Kris with debugging these weird problems, expect to see pull-request today, in around 3-4 hours! It gonna remove many issues ;) all thank you will go to Kris! keep monitoring this topic :)
  • Thanks a million :) I'm sorry I didn't have time to dive into this myself, but I'll be very interested to see the changes and find out what was causing this weird issues :) Again - thanks!
  • Hello :) Actually Kris have fixed not only this bug, but a few more we have found :) here you go :) think it'll get pulled to develop soon:) https://github.com/fuel/core/pull/381
  • Looks good, I'd suggest though to use the Config library for importing saved configs. I'd suggest autoloading a config file 'app/config/image.php' using Config::load('image', true) and have a method for loading additional configs:
    Image::load_config('another_image');
    

    Which uses Config::load($input, 'image') and the config file contains one or multiple groups (array keys) with the config that are added to the main image config group.
  • Thanks guys, everything working fine with gd driver. Any suggestions on how to use imagemagick with wampserver?
  • Hey again, I had time to get the changes from github. I tested and the problem not just persisted but started to show not only in the very wide images (width very larger than height) as before but in all images.
    Luckily I had time and started tracing this. Finally I managed to trace it to the _resize method in the Image/Driver class. More specifically the bcdiv function on line 257 and 258 wasn't calculating the ratios correctly. It always returned 0, and from then on the resize(0,0) would return an empty (black) image.
    It turns out, I don't know why, that bcdiv wouldn't work correctly because the first parameter is string and the second is float. When I typecast the first parameter ($width, $height) to real, everything starts working like a charm. I don't know why that is since in php's documentation bcdiv is listed as accepting strings as parameters?!
    However I'm making a pull request in a few minutes. Can you check it out and see if that seems logical to you, cause I was kinda buffled.
  • Rostislav, please provide some testing code too, so we know what you are doing, and it'll be easier for us to test this out. Basically we tested different combos of crop_resize, resize etc. so controller example would be good :)
  • I'm suspecting this is something specific with the GD version or something like that.
    The php code I'm using to test this is simple:
    $root_path = './assets/uploads/';
    foreach (\Config::get('thumbs') as $preset => $path)
    {
        if ( ! file_exists($root_path.$path) )
        {
            \File::create_dir($root_path, $path);
        }
        \Image::load($root_path.$saved_as)->preset($preset)->save($root_path.$path.'/'.$saved_as);
    }
    

    The thumbs config has preset names and directories where to put the resulting images as an associative array. And these are my test presets:
    'thumb_100' => array(
          'actions' => array(
            array('crop_resize', 100, 100),
          )
        ),
        'thumb_200' => array(
          'actions' => array(
            array('resize', 200),
          )
        ),
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion