예제 #1
0
 public static function createFromFile($filename, $mime_type, $objAlbum)
 {
     $objPicture = new clsPicture();
     /* Decide which incoming mime type it is. */
     switch ($mime_type) {
         case 'image/jpeg':
             $img = ImageCreateFromJpeg($filename);
             break;
         case 'image/png':
             $img = ImageCreateFromPng($filename);
             break;
         case 'image/gif':
             $img = ImageCreateFromGif($filename);
             break;
         default:
             return 'image_filetype';
     }
     list($intWidth, $intHeight) = getImageSize($filename);
     $intMaxWidth = $objAlbum->get('max_width');
     $intMaxHeight = $objAlbum->get('max_height');
     if ($intMaxWidth <= 0) {
         $intMaxWidth = DEFAULT_X;
     }
     if ($intMaxHeight <= 0) {
         $intMaxHeight = DEFAULT_Y;
     }
     if ($intWidth > $intMaxWidth || $intHeight > $intMaxHeight) {
         /* Check whether the image needs to be resized vertically or horizonally more. */
         if ($intWidth / $intMaxWidth > $intHeight / $intMaxHeight) {
             /* Right-left needs to have priority. */
             $ratio = $intMaxWidth / $intWidth;
         } else {
             /* Up-down needs to have priority. */
             $ratio = $intMaxHeight / $intHeight;
         }
         $intNewWidth = $intWidth * $ratio;
         $intNewHeight = $intHeight * $ratio;
         $imgNew = @ImageCreateTrueColor($intNewWidth, $intNewHeight);
         if (!@ImageCopyResized($imgNew, $img, 0, 0, 0, 0, $intNewWidth, $intNewHeight, $intWidth, $intHeight)) {
             return "image_noresize";
         }
         $intWidth = $intNewWidth;
         $intHeight = $intNewHeight;
         ImageDestroy($img);
         $img = $imgNew;
     }
     /* This has to be done before setImage() because setImage() needs data from the album. */
     $objPicture->set('album_id', $objAlbum->get('id'));
     $result = $objPicture->setImage($img);
     ImageDestroy($img);
     if ($result) {
         return $result;
     }
     $objPicture->set('width', $intWidth);
     $objPicture->set('height', $intHeight);
     $objPicture->save();
     return $objPicture;
 }