Hi all my friends :)

I hope you're all doing great :)

I need a small help from you, and I hope you'll not let me down *shy*

I have a script that it shows all images in the directory of my site under the folder (Albums)

for (.jpg) only!

So, I tried to make this script accepts also (.png) formats

Actually, What I did is inserting this code

elseif ( preg_match ( '#.png$#i', $f ) )
            {
                $info = getimagesize ( $path . $f );
                $dimension = $info[0] . 'x' . $info[1];
                $contents['images'][] = array ( 'name' => $f, 'dimension' => $dimension, 'modified' => filemtime ( $path . $f ), 'size' => filesize ( $path . $f ) );

to this code in *FUNCTION.PHP*
the code after inserting my code

function dir_get_contents ( $root, $dir )
{
    $dir = $dir != '' && $dir != '/' ? trim ( $dir, '/' ) : '';
    $path = $root . $dir . '/';
    $h = opendir ( $path );
    if ( !$h )
    {
        exit ( 'Unable to open ' . $path );
    }

    $contents['images'] = array( );

    $contents['albums'] = array ( );

    while ( false != ( $f = readdir ( $h ) ) )
    {
        if ( $f != '.' && $f != '..' )
        {
            if ( is_dir ( $path . $f ) )
            {
                $info = dir_info ( $path . $f );

                $contents['albums'][] = array ( 'name' => $f, 'albums' => $info['dirs'], 'images' => $info['files'] );
            }
            elseif ( preg_match ( '#.jpg$#i', $f ) )
            {
                $info = getimagesize ( $path . $f );
                $dimension = $info[0] . 'x' . $info[1];
                $contents['images'][] = array ( 'name' => $f, 'dimension' => $dimension, 'modified' => filemtime ( $path . $f ), 'size' => filesize ( $path . $f ) );
            }
            elseif ( preg_match ( '#.png$#i', $f ) )
            {
                $info = getimagesize ( $path . $f );
                $dimension = $info[0] . 'x' . $info[1];
                $contents['images'][] = array ( 'name' => $f, 'dimension' => $dimension, 'modified' => filemtime ( $path . $f ), 'size' => filesize ( $path . $f ) );
            }
        }
    }
    return $contents;
}

Now, I ended up with an error
saying

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/********/functions.php on line 217

Now, I tried to do my best in the problem which occures in the line 217
BUT, I couldn't do anything :\

this is the code in line 217 which causes the problem (this code generates thumbnails for the images)

function create_thumbnail ($source, $destination, $new_width = 100, $new_height = 'auto', $quality = 85)
{
    $im_src = imagecreatefromjpeg($source);
    if (!$im_src) return;
    $im_width = imagesX($im_src);
    $im_height = imagesY($im_src);
    if( !is_int($new_width) && is_int($new_height) )
    {
        // resize the image based on height
        $ratio = $im_height / $new_height;
        $new_width = floor($im_width / $ratio);
    }
    elseif( is_int($new_width) && !is_int($new_height) )
    {
        // resize the image based on the width
        $ratio = $im_width / $new_width;
        $new_height = floor($im_height / $ratio);
    }
    // create blank image
    $im = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $new_width, $new_height, $im_width, $im_height);
    imagejpeg($im, $destination, $quality);
    imagedestroy($im);
    imagedestroy($im_src);
}

I wonder if anyone can help me fix this problem, i'll be much more appreciated :)

by the way, this is the WHOLE code for the *FUNCTION.PHP*

<?php
function dir_info ( $dir )
{
    $dir = $dir != '' && $dir != '/' ? trim ( $dir, '/' ) . '/' : '';
    $h = @opendir ( $dir );
    if ( !$h )
    {
        exit ( 'Unable to open ' . $dir );
    }
    $count['dirs'] = 0;
    $count['files'] = 0;
    $count['size'] = 0;
    while ( false != ( $f = readdir ( $h ) ) )
    {
        if ( $f != '.' && $f != '..' )
        {
            if ( is_dir ( $dir . $f ) )
            {
                $count['dirs']++;
            }
            else
            {
                $count['files']++;
                $count['size'] += filesize ( $dir . $f );
            }
        }
    }
    return $count;
}


function dir_get_contents ( $root, $dir )
{
    $dir = $dir != '' && $dir != '/' ? trim ( $dir, '/' ) : '';
    $path = $root . $dir . '/';
    $h = opendir ( $path );
    if ( !$h )
    {
        exit ( 'Unable to open ' . $path );
    }

    $contents['images'] = array( );

    $contents['albums'] = array ( );

    while ( false != ( $f = readdir ( $h ) ) )
    {
        if ( $f != '.' && $f != '..' )
        {
            if ( is_dir ( $path . $f ) )
            {
                $info = dir_info ( $path . $f );

                $contents['albums'][] = array ( 'name' => $f, 'albums' => $info['dirs'], 'images' => $info['files'] );
            }
            elseif ( preg_match ( '#.jpg$#i', $f ) )
            {
                $info = getimagesize ( $path . $f );
                $dimension = $info[0] . 'x' . $info[1];
                $contents['images'][] = array ( 'name' => $f, 'dimension' => $dimension, 'modified' => filemtime ( $path . $f ), 'size' => filesize ( $path . $f ) );
            }
            elseif ( preg_match ( '#.png$#i', $f ) )
            {
                $info = getimagesize ( $path . $f );
                $dimension = $info[0] . 'x' . $info[1];
                $contents['images'][] = array ( 'name' => $f, 'dimension' => $dimension, 'modified' => filemtime ( $path . $f ), 'size' => filesize ( $path . $f ) );
            }
        }
    }
    return $contents;
}


function create_table ( $cell_values, $columns = 4, $table_start = '<table>', $tr_start = '<tr>', $td_start = '<td>', $empty_cell = '<div style="clear:both;float:left;"></div>' )
{
    $i = 0;
    $rows = ceil ( count ( $cell_values ) / $columns );
    $html[] = $table_start;
    for ( $r = 0; $r < $rows; $r++ )
    {
        $html[] = '  ' . $tr_start;
        for ( $c = 0; $c < $columns; $c++ )
        {
            $html[] = '    ' . $td_start . ( isset ( $cell_values[$i] ) ? $cell_values[$i] : $empty_cell ) . '</td>';
            $i++;
        }
        $html[] = '  </tr>';
    }
    $html[] = '</table>';
    return implode ( "\n", $html );
}

function read ( $file )
{
    $fp = @fopen ( $file, 'rb' );
    if ( $fp )
    {
        if ( filesize ( $file ) === 0 )
        {
            return array ( );
        }
        flock ( $fp, 1 );
        return unserialize ( fread ( $fp, filesize ( $file ) ) );
    }
    else
    {
        exit ("Could not open $file for reading.");
    }
}

function write ( $file, $data )
{
    $fp = @fopen ( $file, 'wb' );
    if ( $fp )
    {
        flock ( $fp, 2 );
        fwrite ( $fp, serialize ( $data ) );
        fclose ( $fp );
    }
    else
    {
        exit ("Could not open $file for writting.");
    }
}

function slice ( $str, $width, $pos = 1 )
{
    if ( strlen ( $str ) <= $width )
    {
        return $str;
    }
    if ( $pos === 0 )
    {
        return '...' . substr ( $str, 3, $width - 3 );
    }
    if ( $pos === 1 )
    {
        return substr ( $str, 0, ( $width / 2 ) - 1 ) . '...' . substr ( $str, - ( $width / 2 - 2 ) );
    }
    else
    {
        return substr ( $str, 0, $width - 3 ) . '...';
    }
}

function parse ( &$template, $data, $x = '' )
{
    if ( !is_array ( $data ) )
    {
        return str_replace ($data, $x, $template);
    }
    else
    {
        return str_replace ( array_keys($data), array_values($data), $template );
    }
}

function timer ($start = 0, $digits = 8)
{
    list ($m, $s) = explode(' ', microtime( ) );
    return round ( (double)$s + (double)$m - $start, $digits);
}

function parse_if ( &$template, $varname, $varval, $false_replace = '' )
{
    $$varname = $varval;
    return preg_replace ( '#{if ' . $varname . '}(.+?){endif ' . $varname .'}#ise', '($' . $varname . ')?"$1":"$false_replace"', $template );
}

function make_dir ( $root, $dir, $mode = 0777 )
{
    $o = umask ( 0 );
    $dir = trim ( $dir, '/' );
    $paths = explode ( '/', $dir );
    $gone = array ( );
    for ( $i = 0; $i < count ( $paths ); $i++ )
    {
        $gone[] = $paths[$i];
        $d = $root . implode ( '/', $gone );
        if ( !is_dir ( $d ) )
        {
            mkdir ( $root . implode ( '/', $gone ), $mode );
        }
    }
    umask ( $o );
}

function multisort ($array, $column, $order = 'asc', $type = SORT_STRING)
{
    if (!isset ($array[0][$column]) )
    {
        return $array;
    }
    for ($i = 0; $i < count ($array); $i++)
    {
        $temp[$i] = $array[$i][$column];
    }
    switch ($order)
    {
        default:
        case 'asc':
            $order = SORT_ASC;
        break;
        case 'dsc':
        case 'desc':
            $order = SORT_DESC;
        break;
    }
    array_multisort ($temp, $order, $type , $array);

    return $array;
}


function create_thumbnail ($source, $destination, $new_width = 100, $new_height = 'auto', $quality = 85)
{
    $im_src = imagecreatefromjpeg($source);
    if (!$im_src) return;
    $im_width = imagesX($im_src);
    $im_height = imagesY($im_src);
    if( !is_int($new_width) && is_int($new_height) )
    {
        // resize the image based on height
        $ratio = $im_height / $new_height;
        $new_width = floor($im_width / $ratio);
    }
    elseif( is_int($new_width) && !is_int($new_height) )
    {
        // resize the image based on the width
        $ratio = $im_width / $new_width;
        $new_height = floor($im_height / $ratio);
    }
    // create blank image
    $im = imagecreatetruecolor($new_width, $new_height);
    imagecopyresampled($im, $im_src, 0, 0, 0, 0, $new_width, $new_height, $im_width, $im_height);
    imagejpeg($im, $destination, $quality);
    imagedestroy($im);
    imagedestroy($im_src);
}
?>

Thanks in advance :)

I don't have the time to give you an in depth answer tonight, but I've attached a set of functions that create thumbnails with PHP which I developed semi-recently. Take a look and see what I did that's different from your script, or use the functions if they work for you.

I believe the usage of the functions is (hopefully) self explanatory ...

Hope it helps

Attachments
<?php

// path/to/image_name (without file type)
function get_image_type ( $image_path ) {
  switch ( true ) {
    case file_exists( $image_path.'.png' ) :  return 'png';
    case file_exists( $image_path.'.jpg' ) :  return 'jpg';
    case file_exists( $image_path.'.gif' ) :  return 'gif';
    default: return false;
  }
}

// target quality is optional if passing in jpeg images
function image_scale_by_width ( $source_file_path, $publish_file_path, $target_width, $target_quality = 8 ) {
  if ( file_exists( $source_file_path ) ) {
    $source_info = getimagesize( $source_file_path );
    $source_mime = $source_info['mime'];
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    $target_height = round( ( $target_width / $source_width ) * $source_height );
    switch ( $source_mime ) {
      case 'image/png':
        $source_image = imagecreatefrompng( $source_file_path );
      break;
      case 'image/jpeg':
        $source_image = imagecreatefromjpeg( $source_file_path );
      break;
      case 'image/gif':
        $source_image = imagecreatefromgif( $source_file_path );
      break;
      default:
        print "Could not determine source image type<br>\n";
      return false;
    }
    $scaled_image = imagecreatetruecolor( $target_width, $target_height );
    imagecopyresampled( $scaled_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height );
    imagepng( $scaled_image, $publish_file_path, $target_quality );
    imagedestroy( $source_image );
    imagedestroy( $scaled_image );
  } else {
    return false;
  }
  return file_exists( $publish_file_path );
}

// target quality is optional if passing in jpeg images
function image_scale_by_height ( $source_file_path, $publish_file_path, $target_height, $target_quality = 8 ) {
  if ( file_exists( $source_file_path ) ) {
    $source_info = getimagesize( $source_file_path );
    $source_mime = $source_info['mime'];
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    $target_width = round( ( $target_height / $source_height ) * $source_width );
    switch ( $source_mime ) {
      case 'image/png':
        $source_image = imagecreatefrompng( $source_file_path );
      break;
      case 'image/jpeg':
        $source_image = imagecreatefromjpeg( $source_file_path );
      break;
      case 'image/gif':
        $source_image = imagecreatefromgif( $source_file_path );
      break;
      default:
        print "Could not determine source image type<br>\n";
      return false;
    }
    $scaled_image = imagecreatetruecolor( $target_width, $target_height );
    imagecopyresampled( $scaled_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height );
    imagepng( $scaled_image, $publish_file_path, $target_quality );
    imagedestroy( $source_image );
    imagedestroy( $scaled_image );
  } else {
    return false;
  }
  return file_exists( $publish_file_path );
}

// fill color is optional or defaults to white
function image_trim_fill_edges ( $source_file_path, $publish_file_path, $target_width, $fill_color = array( 255, 255, 255 ) ) {
  if ( file_exists( $source_file_path ) ) {
    $source_info = getimagesize( $source_file_path );
    $source_mime = $source_info['mime'];
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    switch ( $source_mime ) {
      case 'image/png':
        $source_image = imagecreatefrompng( $source_file_path );
      break;
      case 'image/jpeg':
        $source_image = imagecreatefromjpeg( $source_file_path );
      break;
      case 'image/gif':
        $source_image = imagecreatefromgif( $source_file_path );
      break;
      default:
        print "Could not determine source image type<br>\n";
      return false;
    }
    $cropped_image = imagecreatetruecolor( $target_width, $source_height );
    if ( ( $target_width > $source_width ) && $fill_color ) {
      $red = $fill_color[0];
      $green = $fill_color[1];
      $blue = $fill_color[2];
      $image_fill = imagecolorallocate( $cropped_image, $red, $green, $blue );
      imagefill( $cropped_image, 0, 0, $image_fill );
      $left_offset = round( ( $target_width - $source_width ) / 2 );
      imagecopy( $cropped_image, $source_image, $left_offset, 0, 0, 0, $source_width, $source_height );
    } else {
      $width_offset = round( ( $source_width - $target_width ) / 2 );
      imagecopy( $cropped_image, $source_image, 0, 0, $width_offset, 0, $target_width, $source_height );
    }
    imagejpeg( $cropped_image, $publish_file_path );
    imagedestroy( $source_image );
    imagedestroy( $cropped_image );
  } else {
    return false;
  }
  return file_exists( $publish_file_path );
}

// fill color is optional or defaults to white
function image_trim_fill_gutter ( $source_file_path, $publish_file_path, $target_height, $fill_color = array( 255, 255, 255 ) ) {
  if ( file_exists( $source_file_path ) ) {
    $source_info = getimagesize( $source_file_path );
    $source_mime = $source_info['mime'];
    $source_width = $source_info[0];
    $source_height = $source_info[1];
    switch ( $source_mime ) {
      case 'image/png':
        $source_image = imagecreatefrompng( $source_file_path );
      break;
      case 'image/jpeg':
        $source_image = imagecreatefromjpeg( $source_file_path );
      break;
      case 'image/gif':
        $source_image = imagecreatefromgif( $source_file_path );
      break;
      default:
        print "Could not determine source image type<br>\n";
      return false;
    }
    $cropped_image = imagecreatetruecolor( $source_width, $target_height );
    if ( ( $target_height > $source_height ) && $fill_color ) {
      $red = $fill_color[0];
      $green = $fill_color[1];
      $blue = $fill_color[2];
      $image_fill = imagecolorallocate( $cropped_image, $red, $green, $blue );
      imagefill( $cropped_image, 0, 0, $image_fill );
      $top_offset = round( ( $target_height - $source_height ) / 2 );
      imagecopy( $cropped_image, $source_image, 0, $top_offset, 0, 0, $source_width, $source_height );
    } else {
      imagecopy( $cropped_image, $source_image, 0, 0, 0, 0, $source_width, $target_height );
    }
    imagejpeg( $cropped_image, $publish_file_path );
    imagedestroy( $source_image );
    imagedestroy( $cropped_image );
  } else {
    return false;
  }
  return file_exists( $publish_file_path );
}

?>

Since you are trying to use .png files, $im_src = imagecreatefromjpeg($source); will not work. Because, imagecreatefromjpeg will work for jpeg files !
If you want to work with .png files , you should use imagecreatefrompng .

This article has been dead for over six months. Start a new discussion instead.