Imagepng Problems

Issue #96 on hold
Mark Manning created an issue

I wrote a small PHP script to help me to batch process images. It has worked for months without a glitch - until now. The script simply takes in an image and rotates it. The problem is with imagepng. When it tries to save I get a 255 error and nothing (or sometimes a little of the file) is put into the output file. I checked doing the same thing with several other programs (such as Photoshop, Visio, Canvas, Paint.NET, and Painter and none of them had a problem with the image. I switched out imagepng with imagegif and there were no problems saving the file either. So I'm suspecting a problem with imagepng. After this happened - imagepng will no longer work. I've even uninstalled and re-installed PHP and imagepng still will not work. I am using PHP v5.3.23. My system has 2GB of memory and is a Dell 6400. I amusing a 1TB disk drive with around 500GB free. The image is very large so I am going to put it onto my FTP server. You can get the image from there. Use the standard anonymous login with no password. Server's location is

I thought about it and decided to add one more thing: It isn't that imagepng won't work in the PHP script. It is that imagepng will not work period. Since I ran my program and tried to rotate this image imagepng just won't work. Even on simple things like a blank image I just get a 255 error. Even on a 10x10 image - I get the 255 error. So it isn't that it doesn't work in my program. It is like I broke something major in imagepng and now it won't do anything at all. So any ideas on what stupid thing I did would be helpful. :-)

Here is the program:

#   Rot.php
#   A program to rotate images by Mark Manning
#      This program has been GNU'd and anyone can use it however
#      they would like.  Please give credit if you use it for commercial
#      purposes.
    $rot = 0;
    $dir = "";
    $skip = 0;
    $odd = false;
    $even = false;
    foreach( $argv as $k=>$v ){
        if( preg_match("/-o|-odd/i", $v) ){ $odd = true; }
        if( preg_match("/-e|-even/i", $v) ){ $even = true; }
        if( preg_match("/-s|-skip/i", $v) ){ $skip = $argv[$k+1]; }
        if( preg_match("/-r|-rot|-rotate/i", $v) ){ $rot = $argv[$k+1]; }
        if( preg_match("/-d|-dir|-directory/i", $v) ){ $dir = $argv[$k+1]; }

    if( strlen($dir) < 1 ){
        print "Please input the image diretory >";
        $dir = trim( fgets(STDIN) );
        if( strlen($dir) < 1 ){ die( "No directory given...aborting." ); }

    if( abs($rot) == 0 ){ die( "No rotation given...aborting." ); }
#   See how many files there are.
    $dirs = array();
    $files = array();
    if( $dh = opendir($dir) ){
        echo "Directory : $dir ...please wait.\n";
        while( ($file = readdir($dh)) !== false ){
            echo "Looking at : $file ...please wait.\n";
            if( preg_match("/\.png$/i", $file) ){ $files[] = "$dir/$file"; }
            if( !preg_match("/^\./", $file) && is_dir("$dir/$file") ){ $dirs[] = "$dir/$file"; }

        closedir( $dh );

    $num_files = count( $files ) - 1;

    $c = 0;
    foreach( $files as $k=>$v ){
        echo "Working on file : $v ...please wait.\n";
        if( ($skip > 0) && ($c >= $skip) ){ $c++; continue; }
        if( $odd && (($c % 2) < 1) ){ $c++; continue; }
        if( $even && (($c %2) > 0) ){ $c++; continue; }
        $size = getimagesize( $v );
        $old = imagecreatefrompng( $v );
        imagealphablending( $old, false );
        imagesavealpha( $old, true );

        $new = imagerotate( $old, $rot, imageColorAllocateAlpha($old, 0, 0, 0, 127));
        imagealphablending( $new, false );
        imagesavealpha( $new, true );

        imagepng( $new, $v );
        imagedestroy( $old );
        imagedestroy( $new );

Comments (2)

  1. Pierre Joye

    You may reach memory limit or the likes.

    Please create a very simple script with only imagepng and imagecreatefrompng using a static file, run it in the CLI and you may see what actually goes wrong

  2. Log in to comment