/**
  * @dataProvider fileTypeCases
  *
  */
 public function testUploadedMimeType($filename, $expectedType)
 {
     if (!file_exists($filename)) {
         throw new Exception("WTF? {$filename} test file missing");
     }
     $tmp = tmpfile();
     fwrite($tmp, file_get_contents($filename));
     $tmp_metadata = stream_get_meta_data($tmp);
     $type = MediaFile::getUploadedMimeType($tmp_metadata['uri'], basename($filename));
     $this->assertEquals($expectedType, $type);
 }
 /**
  * Handle the request
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     $profile = $this->user->getProfile();
     $base64img = $this->img;
     if (stristr($base64img, 'image/jpeg')) {
         $base64img_mime = 'image/jpeg';
     } elseif (stristr($base64img, 'image/png')) {
         // should convert to jpg here!!
         $base64img_mime = 'image/png';
     }
     $base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
     $base64img = str_replace('data:image/png;base64,', '', $base64img);
     $base64img = str_replace(' ', '+', $base64img);
     $base64img_hash = md5($base64img);
     $base64img = base64_decode($base64img);
     $base64img_basename = basename('avatar');
     $base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime);
     $base64img_path = File::path($base64img_filename);
     $base64img_success = file_put_contents($base64img_path, $base64img);
     $base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename);
     $mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype);
     $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
     $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $this->cropW, 'height' => $this->cropH, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH));
     $type = $imagefile->preferredType();
     $filename = Avatar::filename($profile->id, image_type_to_extension($type), null, common_timestamp());
     $filepath = Avatar::path($filename);
     $imagefile->copyTo($filepath);
     $profile = $this->user->getProfile();
     $profile->setOriginal($filename);
     $mediafile->delete();
     $twitter_user = $this->twitterUserArray($profile, true);
     $this->initDocument('json');
     $this->showJsonObjects($twitter_user);
     $this->endDocument('json');
 }
 /**
  * Handle the request
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     $profile = $this->user->getProfile();
     $base64img = $this->img;
     if (stristr($base64img, 'image/jpeg')) {
         $base64img_mime = 'image/jpeg';
     } elseif (stristr($base64img, 'image/png')) {
         // should convert to jpg here!!
         $base64img_mime = 'image/png';
     }
     $base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
     $base64img = str_replace('data:image/png;base64,', '', $base64img);
     $base64img = str_replace(' ', '+', $base64img);
     $base64img_hash = md5($base64img);
     $base64img = base64_decode($base64img);
     $base64img_basename = basename('bg');
     $base64img_filename = File::filename($profile, $base64img_basename, $base64img_mime);
     $base64img_path = File::path($base64img_filename);
     $base64img_success = file_put_contents($base64img_path, $base64img);
     $base64img_mimetype = MediaFile::getUploadedMimeType($base64img_path, $base64img_filename);
     $mediafile = new MediaFile($profile, $base64img_filename, $base64img_mimetype);
     $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
     $imagefile->resizeTo(File::path($mediafile->filename), array('width' => 1280, 'height' => 1280, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH));
     $result['url'] = File::url($mediafile->filename);
     Profile_prefs::setData($profile, 'qvitter', 'background_image', $result['url']);
     $this->initDocument('json');
     $this->showJsonObjects($result);
     $this->endDocument('json');
 }
 /**
  * Handle the request
  *
  * @return void
  */
 protected function handle()
 {
     parent::handle();
     $profile = $this->user->getProfile();
     // see if we have regular uploaded image data
     try {
         $mediafile = MediaFile::fromUpload('banner', $profile);
     } catch (NoUploadedMediaException $e) {
         // if not we may have base64 data
         $img = $this->img;
         if (stristr($img, 'image/jpeg')) {
             $img_mime = 'image/jpeg';
         } elseif (stristr($img, 'image/png')) {
             // should convert to jpg here!!
             $img_mime = 'image/png';
         }
         // i don't remember why we had to do this
         $img = str_replace('data:image/jpeg;base64,', '', $img);
         $img = str_replace('data:image/png;base64,', '', $img);
         $img = str_replace(' ', '+', $img);
         $img = base64_decode($img, true);
         try {
             $img_filename = File::filename($profile, 'cover', $img_mime);
             $img_path = File::path($img_filename);
             $img_success = file_put_contents($img_path, $img);
             $img_mimetype = MediaFile::getUploadedMimeType($img_path, $img_filename);
             $mediafile = new MediaFile($profile, $img_filename, $img_mimetype);
         } catch (Exception $e) {
             $this->clientError($e, 400);
         }
     }
     if (!$mediafile instanceof MediaFile) {
         $this->clientError(_('Could not process image data.'), 400);
     }
     // maybe resize
     $width = $this->cropW;
     $height = $this->cropH;
     $scale = 1;
     if ($width > 1200) {
         $scale = 1200 / $width;
     } elseif ($height > 600) {
         $scale = 600 / $height;
     }
     $width = round($width * $scale);
     $height = round($height * $scale);
     // crop
     try {
         $imagefile = new ImageFile($mediafile->fileRecord->id, File::path($mediafile->filename));
         $imagefile->resizeTo(File::path($mediafile->filename), array('width' => $width, 'height' => $height, 'x' => $this->cropX, 'y' => $this->cropY, 'w' => $this->cropW, 'h' => $this->cropH));
         $result['url'] = File::url($mediafile->filename);
     } catch (Exception $e) {
         $this->clientError(_('The image could not be resized and cropped. ' . $e), 422);
     }
     // save in profile_prefs
     try {
         Profile_prefs::setData($profile, 'qvitter', 'cover_photo', $result['url']);
     } catch (ServerException $e) {
         $this->clientError(_('The image could not be resized and cropped. ' . $e), 422);
     }
     // return json
     $this->initDocument('json');
     $this->showJsonObjects($result);
     $this->endDocument('json');
 }