Announcement

  •  » Extensions
  •  » MOD to resize uploaded images

#1 2005-12-03 12:32:10

chrisaga
Former Piwigo Team
France (92)
2005-08-10
543

MOD to resize uploaded images

I made a little hack for my personal gallery to resize uploaded images which are too big instead of sending an error.

The big image is copied to pwg_high and I use GD to compute a new image which fits in $conf['upload_maxheight'] x $conf['upload_maxwidth'].

I've not made a real MOD for this but I give the code here in case it would be usefull, and someone would make the MOD. With a little more integration (messages for the end user, ...) it could be a new feature for next version of PWG (of course with an activation option for the administrator). What do you think ?

include/functions_img.inc.php (new file) :

Code:

<?php
function cpResample($path, $newpath, $newWidth, $newHeight)
{
  global $lang;

  $filename = basename($path);
  $dirname = dirname($path);
  $newdir = dirname($newpath);
  
  // extension of the picture filename
  $extension = get_extension($filename);

  // create image from file
  if ($extension == 'jpg' or $extension == 'JPG')
  {
    $srcImage = @imagecreatefromjpeg($path);
  }
  else if ($extension == 'png' or $extension == 'PNG')
  {
    $srcImage = @imagecreatefrompng($path);
  }
  else
  {
    unset($extension);
  }

  if ( isset( $srcImage ) )
  {
    // width/height
    $srcWidth    = imagesx( $srcImage ); 
    $srcHeight   = imagesy( $srcImage ); 
    $ratioWidth  = $srcWidth/$newWidth;
    $ratioHeight = $srcHeight/$newHeight;

    // maximal size exceeded ?
    if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) )
    {
      if ( $ratioWidth < $ratioHeight)
      { 
        $destWidth = $srcWidth/$ratioHeight;
        $destHeight = $newHeight; 
      }
      else
      { 
        $destWidth = $newWidth; 
        $destHeight = $srcHeight/$ratioWidth;
      }
    }
    else
    {
      $destWidth = $srcWidth;
      $destHeight = $srcHeight;
    }
      // GD 2.0 or more recent -> good results (but slower)
      $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
      imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
                          $destWidth,$destHeight,$srcWidth,$srcHeight );
    // creation and backup of final picture
    if (!is_writable($newdir))
    {
      array_push($page['errors'], '['.$newdir.'] : '.$lang['no_write_access']);
      return false;
    }
    imagejpeg($destImage, $newpath);
    // freeing memory ressources
    imagedestroy( $srcImage );
    imagedestroy( $destImage );
    
    list($img_width, $img_height) = getimagesize($newpath);
    $img_size = floor(filesize($newpath) / 1024).' KB';
    
    $info = array( 'path'      => $path,
                   'big_file'   => $newpath,
                   'img_width'  => $img_width,
                   'img_height' => $img_height,
                   'img_size'   => $img_size );
    return $info;
  }
  // error
  else
  {
  // TODO : error messages
    exit();
  }
}
?>

upload.php (mods) :

Code:

function validate_upload( $temp_name, $my_max_file_size,
                          $image_max_width, $image_max_height )
{
  global $conf, $lang;
    
  $result = array();
  $result['error'] = array();
  $extension = get_extension( $_FILES['picture']['name'] );
  if (!in_array($extension, $conf['picture_ext']))
  {
    array_push( $result['error'], $lang['upload_advise_filetype'] );
    return $result;
  }
  if ( !isset( $_FILES['picture'] ) )
  {
    // do we even have a file?
    array_push( $result['error'], "You did not upload anything!" );
  }
  else if ( $_FILES['picture']['size'] > $my_max_file_size * 1024 )
  {
    array_push( $result['error'],
                $lang['upload_advise_filesize'].$my_max_file_size.' KB' );
  }
  else
  {
    // check if we are allowed to upload this file_type
    // upload de la photo sous un nom temporaire
    if ( !move_uploaded_file( $_FILES['picture']['tmp_name'], $temp_name ) )
    {
      array_push( $result['error'], $lang['upload_cannot_upload'] );
    }
    else
    {
      $size = getimagesize( $temp_name );
// chrisaga start
      $high_path = dirname($temp_name).'/pwg_high/'.basename($temp_name);
      if ( ( isset( $image_max_width ) and isset( $image_max_height ) )
           and ( ( $image_max_width != ""
                   and $size[0] > $image_max_width )
                 or ( $image_max_height != ""
                      and $size[1] > $image_max_height ) ) )
      {
         // copy img to pwg_high and make a smaller image
         @copy( $temp_name, $high_path) ;
         cpResample($high_path, $temp_name, $image_max_width, $image_max_height);
      }
      else
      {
// chrisaga end
        if ( isset( $image_max_width )
             and $image_max_width != ""
             and $size[0] > $image_max_width )
        {
          array_push( $result['error'],
                      $lang['upload_advise_width'].$image_max_width.' px' );
        }
        if ( isset( $image_max_height )
             and $image_max_height != ""
             and $size[1] > $image_max_height )
        {
          array_push( $result['error'],
                      $lang['upload_advise_height'].$image_max_height.' px' );
        }
// chrisaga start
      }
// chrisaga end
      // $size[2] == 1 means GIF
      // $size[2] == 2 means JPG
      // $size[2] == 3 means PNG
      switch ( $size[2] )
      {
      case 1 : $result['type'] = 'gif'; break;
      case 2 : $result['type'] = 'jpg'; break;
      case 3 : $result['type'] = 'png'; break;
      default :
        array_push( $result['error'], $lang['upload_advise_filetype'] );  
      }
    }
  }
  if ( sizeof( $result['error'] ) > 0 )
  {
    // destruction de l'image avec le nom temporaire
    @unlink( $temp_name );
  }
  else
  {
    @chmod( $temp_name, 0644);
  }
  return $result;
}

cepatre.net
Utilisateur depuis la version 1.3, Impliqué depuis la 1.4, Responsable du template des 1.5 et 1.6  ... et en (in)disponibilité sur la 1.7. J'ai fait l'impasse, et j'ai sauté directement en 2.2.2 !

Offline

 

#2 2005-12-03 15:30:27

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: MOD to resize uploaded images

As a member of PhpWebGallery you have to show the right way : please, create a real MOD :-)

Offline

 

#3 2005-12-03 15:39:40

chrisaga
Former Piwigo Team
France (92)
2005-08-10
543

Re: MOD to resize uploaded images

z0rglub wrote:

As a member of PhpWebGallery you have to show the right way : please, create a real MOD :-)

I was expecting such an answer from you, but i'm working on theme/template splitting right now and if we decide to add this as a feature in next version, i'll never make the MOD.
As the boss of PhpWebGallery, please answer this question : could it be a new feature ?

It still needs some work to become a real MOD, and i'm not very fast because I'm new to both PHP and PhpWebGallery PHP code.


cepatre.net
Utilisateur depuis la version 1.3, Impliqué depuis la 1.4, Responsable du template des 1.5 et 1.6  ... et en (in)disponibilité sur la 1.7. J'ai fait l'impasse, et j'ai sauté directement en 2.2.2 !

Offline

 

#4 2005-12-03 19:38:33

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: MOD to resize uploaded images

chrisaga wrote:

[...] could it be a new feature ?

Yes, I had not read enough your modifications but I think it would be great to add it for the next branch (not in branch 1.5 I mean). In fact I encourage you to add your modification because I'm personnaly not interested in upload feature while you obviously are. I would be glad you maintain this feature :-)

Offline

 

#5 2005-12-03 20:06:36

chrisaga
Former Piwigo Team
France (92)
2005-08-10
543

Re: MOD to resize uploaded images

z0rglub wrote:

In fact I encourage you to add your modification because I'm personnaly not interested in upload feature while you obviously are.

Not so much but my users are :-))

z0rglub wrote:

I would be glad you maintain this feature :-)

OK, you just trapped me on the PHP side of the force ;-)


cepatre.net
Utilisateur depuis la version 1.3, Impliqué depuis la 1.4, Responsable du template des 1.5 et 1.6  ... et en (in)disponibilité sur la 1.7. J'ai fait l'impasse, et j'ai sauté directement en 2.2.2 !

Offline

 

#6 2005-12-05 17:45:25

Jackson Yap
Guest

Re: MOD to resize uploaded images

I really need this mod :(

seems like no one is working on any mod for lastest release for 1.5? :(

 

#7 2005-12-05 18:13:36

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

Re: MOD to resize uploaded images

Jackson Yap wrote:

seems like no one is working on any mod for lastest release for 1.5? :(

It only seems, I can assure you a lot of work is being done on 1.5 extensions. Maybe it's not "seenable" enough, maybe...

Offline

 

#8 2005-12-05 21:46:17

chrisaga
Former Piwigo Team
France (92)
2005-08-10
543

Re: MOD to resize uploaded images

Jackson Yap wrote:

I really need this mod :(

seems like no one is working on any mod for lastest release for 1.5? :(

OK, tell us a little bit about what you expect from this MOD and I may be able to do something for you.
It might not be obvious but there is still some work to turn my little hack into a true reliable MOD


cepatre.net
Utilisateur depuis la version 1.3, Impliqué depuis la 1.4, Responsable du template des 1.5 et 1.6  ... et en (in)disponibilité sur la 1.7. J'ai fait l'impasse, et j'ai sauté directement en 2.2.2 !

Offline

 

#9 2005-12-06 08:00:38

Jackson Yap
Guest

Re: MOD to resize uploaded images

Yes. I love phpwebgallery very much, and you can see how I have used it on n282.apc.sg.

however I am looking for some mods which is available for last few versions but not now yet (I am not advanced in php :()

I am looking for an upload mod that auto resize image to the max size that I allowed and also auto create a thumbnail for it if possible and not always depending on me to create thumbnails.

Another mod which I need will be to allow multiple uploads, and to enable my users to "create" the category they want, so that the category will be created when they upload the photos.

I like phpwebgallery because it is simple, however I also require additional permissions for my users to upload into their proposed category and even upload without permission from me if I have granted their group permission already.

Any suggestion?

Thank you!

 

#10 2005-12-06 19:36:12

chrisaga
Former Piwigo Team
France (92)
2005-08-10
543

Re: MOD to resize uploaded images

Let's continue on this topic [Evolution]Upload wich I started to discuss future specifications of the upload feature.


cepatre.net
Utilisateur depuis la version 1.3, Impliqué depuis la 1.4, Responsable du template des 1.5 et 1.6  ... et en (in)disponibilité sur la 1.7. J'ai fait l'impasse, et j'ai sauté directement en 2.2.2 !

Offline

 
  •  » Extensions
  •  » MOD to resize uploaded images

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact