[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cobalt-users] Image Magick or NetPBM on RaQ4i



> a number of the projects I'm working on at the moment require me to be
able
> to automatically manipulate images once they've been uploaded via PHP,
> specifically I need to have them saved to a preset width and duplicated in
> thumbnail form, then the image details loaded into a MySQL db (that bit
> isn't exactly a problem).
> The GDLib appears to not have the ficility to resize and so after much
> research I've fallen onto 2 options: ImageMagick or NetPBM.

Si-

While not addressing your question on ImageMagick, I am using GDLib to
do exactly the same thing!

Here's a snippet accepting the input from a form, and then generating the
thumb and re-sized (only if too large) main photo.  It does assume a .jpg
format - but additional code could be added to use GIF/PNG functions
based on filetype or a user specified filetype.  That, and the possibility
to
add support for movies/sound/whatever is the purpose of the as-yet
unused $media_type variable.

-- Paul

function process_upload() {
     global $media_type, $media_creator, $media_date, $media_desc,$userfile,
$roll_id, $MAX_FILE_SIZE, $PHP_SELF;

     $media_desc = addslashes($media_desc);

     if (is_uploaded_file($userfile)) {
          $query = "INSERT into media VALUES (NULL,
'$media_type','$media_date', NULL, '$media_desc', '$media_creator',
'$roll_id')";

           $result = mysql_query($query);
           if(!$result) error_message(sql_error());

          echo "Database insert successful!<br>";
          echo "Processing file...";

          $query = "SELECT LAST_INSERT_ID() from media";

          $result = mysql_query($query);
          if(!$result) error_message(sql_error());

          while($query_data = mysql_fetch_array($result)) {
               $file_name = $query_data[0];
          }

          echo "stored as $file_name<br>";

          move_uploaded_file($userfile, "./full/" . $file_name . ".jpg");
          chmod("./full/" . $file_name . ".jpg", 0666);

// thumbnails are 70x100 or 100x70 depending on orig orientation
          $tdim = 100;
          $tqual = 70;

          $image_info = GetImageSize("./full/" . $file_name . ".jpg");
          $swidth = $image_info[0];
          $sheight = $image_info[1];

// landscape
          if($swidth > $sheight) {
               $scale = ceil($swidth/$tdim);
          }

// portrait
          else {
               $scale = ceil($sheight/$tdim);
          }

          $twidth = floor($swidth/$scale);
          $theight = floor($sheight/$scale);

// add a white border
          $top_bord = floor(($tdim - $theight)/2);
          $left_bord = floor(($tdim - $twidth)/2);

          $source =  ImageCreateFromJPEG("./full/" . $file_name . ".jpg");

          $thumb = ImageCreate($tdim,$tdim);
          $bg_color = ImageColorAllocate($thumb,255,255,255);
          ImageFill($thumb,0,0,$bg_color);

ImageCopyResized($thumb,$source,$left_bord,$top_bord,0,0,$twidth,$theight,$s
width,$sheight);
          ImageJPEG($thumb,"./full/" . $file_name . "-thumb.jpg",$tqual);

// full size image width is 500x??? whatever scale dictates
          if($swidth >500) {

               $hdim = 500;
               $tqual = 90;

               $scale = ($swidth/$hdim);

               $twidth = floor($swidth/$scale);
               $theight = floor($sheight/$scale);

               $source =  ImageCreateFromJPEG("./full/" . $file_name .
".jpg");

               $medium = ImageCreate($twidth,$theight);
               $bg_color = ImageColorAllocate($thumb,255,255,255);
               ImageFill($medium,0,0,$bg_color);

ImageCopyResized($medium,$source,0,0,0,0,$twidth,$theight,$swidth,$sheight);
               ImageJPEG($medium,"./full/" . $file_name .
"-med.jpg",$tqual);

               unlink("./full/" . $file_name . ".jpg");
          }

// Original is small enough so we don't muck with it...
          else {
               rename("./full/" . $file_name . ".jpg","./full/" . $file_name
. "-med.jpg");
          }

// Show off our handiwork
          echo "<img src=\"./full/$file_name-thumb.jpg\" width=\"100\"
height=\"100\"><hr><img src=\"./full/$file_name-med.jpg\" width=\"$twidth\"
height=\"$theight\"><hr>";

      }
     else {
          echo "Possible file upload attack: filename $upfile_name.";
     }
}