0

I'm trying to flip an image with php gd function imageflip(). I have GD installed. I'm using WAMP and codeigniter on Windows. (and it's kind of difficult to implement imagemagick on wamp - so I would prefer not to). imageflip() should work as I see it. I'm using PHP version 5.4.16.

Maybe I'm missing something...

from phpinfo():

gd

GD Support          enabled
GD Version          bundled (2.1.0 compatible)
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.4.10
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support        enabled
libJPEG Version     8
PNG Support         enabled
libPNG Version      1.2.50
WBMP Support        enabled
XPM Support         enabled 
libXpm Version      30411
XBM Support         enabled

Directive               Local Value     Master Value
gd.jpeg_ignore_ warning     0               0

I have this code:

foreach($products_settings as &$ps) {                
    $filename = $_SERVER['DOCUMENT_ROOT'] . $ps['product_file'];                
    $source_image = imagecreatefromjpeg($filename);  

    //Put source image-resource into array
    $ps['source_image'] = $source_image;
}

and it works..

When I use imagerotate() it works as well...

foreach($products_settings as &$ps) {                
    $filename = $_SERVER['DOCUMENT_ROOT'] . $ps['product_file'];                
    $source_image = imagecreatefromjpeg($filename);  

    //rotate image
    $angle = $ps['product_angle'];
    if (intval($angle) <> 0) {
        $source_image = imagerotate($source_image, 360-$angle, imageColorAllocateAlpha($source_image, 255, 255, 255, 127));
    }

    //Put source image-resource into array
    $ps['source_image'] = $source_image;
}

...BUT when I use imageflip ( http://php.net/manual/en/function.imageflip.php )

foreach($products_settings as &$ps) {                
    $filename = $_SERVER['DOCUMENT_ROOT'] . $ps['product_file'];                
    $source_image = imagecreatefromjpeg($filename);  
    imageflip($source_image, IMG_FLIP_BOTH);   //code execution stops here

    //Put source image-resource into array
    $ps['source_image'] = $source_image;
}  

then the imageflip() makes the code stop (If I add log message the row after it doesn't log). I can't figure out WHY? What would be the reason for that? I can't see anything in errorlogs in codeigniter/php/apache. The manual says that the function will return false when fail, but I know the image is correct because I'm using other gd function for the same image with other gd-functions (as imagerotate, imageColorAllocateAlpha etc)

I've also tried this:

try {
    imageflip($source_image, IMG_FLIP_BOTH); 
}
catch (Exception $e) {
    log_message('DEBUG', 'img err' . $e->__toString());
}

But nothings get logged...

How to debug this?

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
  • Is it possible to surround it with a try/catch? And see if there is an exception thrown? – ScottJShea Sep 01 '13 at 09:20
  • @ScottJShea - I've tried that and I don't get any error... – bestprogrammerintheworld Sep 01 '13 at 09:26
  • Please enable error logging and follow the error log. It will tell you the reason. See as well: [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) - And actually it's not returning false (as you have put in the title), you script just crashes because you called a function that does not exist. That simple it is (well if you know where to look it's always simple, so no pun intended :D) – hakre Sep 01 '13 at 09:31
  • @hakre - I do have error logging enabled in codeigniter, but for some reason when this function did not exists it didn't log anything. (I usally do get warnings/errors into the log) – bestprogrammerintheworld Sep 01 '13 at 09:53
  • I was talking about PHP error logging. As far as Codeigniter is concerned, the more you can disable by it's functioniality replacing it to PHP core functionality the better I'd say because then you're more in control (or you need to learn about how that error logging in CI works compared to PHP so that you can achieve the same by doing more) – hakre Sep 01 '13 at 10:01

1 Answers1

1

There is no imageflip function in PHP version 5.4.16. Install PHP 5.5 and you can use that function.

If you can not install PHP 5.5 and you need to flip an image with GD, you can also use some code that works with older PHP versions, I googled a bit for you:

I'm not totally d'accord with the code for some minor issues, but it actually shows well how this can be done.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • ah of course! I didn't think of that the function did exist, because I expected codeigniter to give me an error in that case, because I do get warnings/errors into the log, but for some reason not in this case. I know there are other solutions like using imagecopy(), imagemagick, using CE Image for codeigniter etc, but I thought a convinient way was to use the gd-functions that are there by default. It didn't really think of my php-version. Thanks a lot! :-) – bestprogrammerintheworld Sep 01 '13 at 09:52
  • 1
    codeigniter is not able to catch fatal errors. That simple it is :D this is why PHP error logging is so important. See as well: [set_error_handler() doens't work for FATAL error](http://stackoverflow.com/q/8527894/367456) (Codeigniter is just using that function) – hakre Sep 01 '13 at 10:02
  • aha. Then things makes sense even I don't understand why fatal erros aren't logged by default in codeigniter (fatal errors are not always that obvious). Thanks for clarifying this! :-) – bestprogrammerintheworld Sep 01 '13 at 10:08
  • The reason is that Codeigniter is written in PHP and when a fatal error in PHP occurs, the PHP is shut down with all PHP code and that code is the error handler by Codeigniter. It can't work and creating the impression to you so far it could work is counterproductive because as you ask here your question you have many question marks even it is just a simple error. – hakre Sep 01 '13 at 10:29