예제 #1
0
 public function execute()
 {
     global $wgRequest;
     wfProfileIn(__METHOD__);
     extract($this->extractRequestParams());
     $imageServing = new ImageServing(array($Id), $Size, array("w" => $Size, "h" => $Height));
     foreach ($imageServing->getImages(1) as $key => $value) {
         $tmpTitle = Title::newFromText($value[0]['name'], NS_FILE);
         $image = wfFindFile($tmpTitle);
         // BugId:31460 ForeignAPIFile does not support loading metadata from the file itself.
         // Note, that ForeignAPIFile::getPath() is a dommy method and always returns false, so
         // the 'File not found' dieUsage() call in the next if block is inevitable for ForeignAPIFile objects.
         if ($FailOnFileNotFound && false == $image instanceof ForeignAPIFile) {
             $image->loadFromFile();
             // side effect forces isMissing() check to fail if file really does not exist
         }
         if (!($image instanceof File && $image->exists()) || $image->isMissing() || $image->mime == 'unknown/unknown') {
             $this->dieUsage('File not found', 'filenotfound');
         }
         $width = $image->getWidth();
         $height = $image->getHeight();
         $imageUrl = $imageServing->getUrl($image->getName(), $width, $height);
     }
     $result = $this->getResult();
     $result->addValue('image', $this->getModuleName(), $imageUrl);
     $result->addValue('imagepage', $this->getModuleName(), $tmpTitle->getFullUrl());
     wfProfileOut(__METHOD__);
 }
예제 #2
0
 private function checkCrop(LocalFile $image)
 {
     $im = new ImageServing(null, 150);
     $crop = $im->getUrl($image, 250, 250);
     // take 250x250 square from original image and scale it down to 150px (width)
     $this->assertContains('150px-0%2C251%2C0%2C250-', $crop, 'Cropped URL is correct');
     $this->assertTrue(Http::get($crop, 'default', ['noProxy' => true]) !== false, 'Crop should return HTTP 200 - ' . $crop);
 }
 public function testCropping()
 {
     // requested crop size is 50 x 50
     $im = new ImageServing(null, 50);
     $file = wfFindFile(self::FILE_NAME);
     // pass dimensions of full size image
     $cropUrl = $im->getUrl($file, $file->getWidth(), $file->getHeight());
     $this->assertContains('/firefly/images/8/89/Wiki-wordmark.png/revision/latest/', $cropUrl);
     $this->assertContains('/width/50/', $cropUrl);
     // verify crop response
     $res = Http::get($cropUrl, 'default', ['noProxy' => true]);
     $this->assertTrue($res !== false, "<{$cropUrl}> should return HTTP 200");
     // verify crop size
     $this->tmpFile = tempnam(wfTempDir(), 'img');
     file_put_contents($this->tmpFile, $res);
     list($tmpWidth, $tmpHeight) = getimagesize($this->tmpFile);
     $this->assertEquals(50, $tmpWidth, 'expected crop width not matched - ' . $cropUrl);
     $this->assertEquals(49, $tmpHeight, 'expected crop height not matched - ' . $cropUrl);
 }
 /**
  * @param array $wikiInfo
  * @param int $width Image width
  * @param int $height Image height
  * @return array
  */
 protected function getImageData($wikiInfo, $width = null, $height = null)
 {
     $imageName = $wikiInfo['image'];
     $crop = $width != null || $height != null;
     $width = $width !== null ? $width : static::DEFAULT_WIDTH;
     $height = $height !== null ? $height : static::DEFAULT_HEIGHT;
     $imgWidth = null;
     $imgHeight = null;
     $img = wfFindFile($imageName);
     if ($img instanceof WikiaLocalFile) {
         //found on en-corporate wiki
         $imgWidth = $img->getWidth();
         $imgHeight = $img->getHeight();
         if ($crop) {
             //get original image if no cropping
             $imageServing = new ImageServing(null, $width, $height);
             $imgUrl = $imageServing->getUrl($img, $width, $height);
         } else {
             $imgUrl = $img->getFullUrl();
         }
     } else {
         $f = $this->findGlobalFileImage($imageName, $wikiInfo['lang'], $wikiInfo['id']);
         if ($f && $f->exists()) {
             $imgWidth = $f->getWidth();
             $imgHeight = $f->getHeight();
             if ($crop) {
                 $globalTitle = $f->getTitle();
                 $imageService = new ImagesService();
                 $response = $imageService->getImageSrc($globalTitle->getCityId(), $globalTitle->getArticleID(), $width, $height);
                 $imgUrl = $response['src'];
             } else {
                 $imgUrl = $f->getUrl();
             }
         }
     }
     if (isset($imgUrl)) {
         return ['image' => $imgUrl, 'original_dimensions' => ['width' => $imgWidth, 'height' => $imgHeight]];
     }
     return ['image' => ''];
 }
 /**
  * Render slider preview
  *
  * @author Jakub Kurcek
  */
 public static function renderSliderPreview($slider)
 {
     global $wgTitle, $wgParser;
     wfProfileIn(__METHOD__);
     // use global instance of parser (RT #44689 / RT #44712)
     $parserOptions = new ParserOptions();
     wfDebug(__METHOD__ . "\n" . print_r($slider, true));
     // render slider images
     foreach ($slider['images'] as &$image) {
         $imageTitle = Title::newFromText($image['name'], NS_FILE);
         $image['pageTitle'] = '';
         $img = wfFindFile($imageTitle);
         if (is_object($img) && $imageTitle->getNamespace() == NS_FILE) {
             // render thumbnail
             $is = new ImageServing(null, self::STRICT_IMG_WIDTH_PREV, array('w' => self::STRICT_IMG_WIDTH_PREV, 'h' => self::STRICT_IMG_HEIGHT_PREV));
             $image['thumbnailBg'] = $is->getUrl($image['name'], $img->getWidth(), $img->getHeight());
         } elseif (is_object($imageTitle)) {
             $image['pageTitle'] = $imageTitle->getText();
         }
         $image['isFileTypeVideo'] = WikiaFileHelper::isFileTypeVideo($img);
         //need to use parse() - see RT#44270
         $image['caption'] = $wgParser->parse($image['caption'], $wgTitle, $parserOptions)->getText();
         // remove <p> tags from parser caption
         if (preg_match('/^<p>(.*)\\n?<\\/p>\\n?$/sU', $image['caption'], $m)) {
             $image['caption'] = $m[1];
         }
     }
     wfDebug(__METHOD__ . '::after' . "\n" . print_r($slider, true));
     // render gallery HTML preview
     $template = new EasyTemplate(dirname(__FILE__) . '/templates');
     $template->set_vars(array('height' => self::STRICT_IMG_HEIGHT_PREV, 'slider' => $slider, 'width' => self::STRICT_IMG_WIDTH_PREV));
     $html = $template->render('sliderPreview');
     wfProfileOut(__METHOD__);
     return $html;
 }
예제 #6
0
 /**
  * @param $outputModel
  * @return mixed
  */
 protected function getImage($wikiId, $articleId)
 {
     $dbName = '';
     try {
         $row = \WikiFactory::getWikiByID($wikiId);
         if ($row) {
             $dbName = $row->city_dbname;
             if (!empty($dbName)) {
                 $db = wfGetDB(DB_SLAVE, [], $dbName);
                 // throws if database does not exits.
                 $imageServing = new \ImageServing([$articleId], self::IMAGE_SIZE, self::IMAGE_SIZE, $db);
                 $isResult = $imageServing->getImages(1);
                 $images = isset($isResult[$articleId]) ? $isResult[$articleId] : false;
                 if ($images && sizeof($images) > 0) {
                     $imageName = $images[0]['name'];
                     $file = \GlobalFile::newFromText($imageName, $wikiId);
                     if ($file->exists()) {
                         return $imageServing->getUrl($file, $file->getWidth(), $file->getHeight());
                     }
                 }
             }
         }
     } catch (\DBConnectionError $ex) {
         // Swallow this exception. there is no simple way of telling if database does not exist other than catching exception.
         // Or am I wrong ?
         \Wikia::log(__METHOD__, false, "Cannot get database connection to " . $dbName);
     }
     return null;
 }
예제 #7
0
 /**
  * @param array $wikiIds
  * @param int $imageWidth
  * @param int $imageHeight
  *
  * @return mixed|null|string
  */
 public function getWikiImages($wikiIds, $imageWidth, $imageHeight = self::IMAGE_HEIGHT_KEEP_ASPECT_RATIO)
 {
     $images = array();
     try {
         $db = wfGetDB(DB_SLAVE, array(), $this->wg->ExternalSharedDB);
         $tables = array('city_visualization');
         $fields = array('city_id', 'city_lang_code', 'city_main_image');
         $conds = array('city_id' => $wikiIds);
         $results = $db->select($tables, $fields, $conds, __METHOD__, array(), array());
         while ($row = $results->fetchObject()) {
             $promoImage = PromoImage::fromPathname($row->city_main_image);
             $promoImage->ensureCityIdIsSet($row->city_id);
             $file = $promoImage->corporateFileByLang($row->city_lang_code);
             if ($file->exists()) {
                 $imageServing = new ImageServing(null, $imageWidth, $imageHeight);
                 $images[$row->city_id] = ImagesService::overrideThumbnailFormat($imageServing->getUrl($file, $file->getWidth(), $file->getHeight()), ImagesService::EXT_JPG);
             }
         }
     } catch (Exception $e) {
         Wikia::log(__METHOD__, false, $e->getMessage());
     }
     return $images;
 }
예제 #8
0
 /**
  * Returns URL to cropped image
  *
  * Uses ImageServing cropping functionality
  *
  * @param int $width requsted width
  * @param int $height requsted height
  * @return string URL
  */
 public function getCrop($width, $height)
 {
     $imageServing = new ImageServing(null, $width, $height);
     return $imageServing->getUrl($this, $this->getWidth(), $this->getHeight());
 }
 /**
  * Return a HTML representation of the image slider
  *
  * @author Jakub Kurcek
  */
 private function renderSlider()
 {
     wfProfileIn(__METHOD__);
     // do not render empty sliders
     if (empty($this->mImages)) {
         wfProfileOut(__METHOD__);
         return '';
     }
     $orientation = $this->getParam('orientation');
     // setup image serving for "big" images
     if ($orientation == 'mosaic') {
         $imagesDimensions = array('w' => WikiaPhotoGalleryHelper::SLIDER_MOSAIC_MIN_IMG_WIDTH, 'h' => WikiaPhotoGalleryHelper::SLIDER_MOSAIC_MIN_IMG_HEIGHT);
     } else {
         $imagesDimensions = array('w' => WikiaPhotoGalleryHelper::SLIDER_MIN_IMG_WIDTH, 'h' => WikiaPhotoGalleryHelper::SLIDER_MIN_IMG_HEIGHT);
     }
     /* temp transition code until grid is fully rolled out, remove and integrate after transition */
     global $wgOasisGrid;
     if ($orientation == 'mosaic' && !empty($wgOasisGrid)) {
         $imagesDimensions['w'] = WikiaPhotoGalleryHelper::WIKIA_GRID_SLIDER_MOSAIC_MIN_IMG_WIDTH;
     }
     /* end temp transistion code */
     $imageServingForImages = new ImageServing(null, $imagesDimensions['w'], $imagesDimensions);
     // setup image serving for navigation thumbnails
     if ($orientation == 'mosaic') {
         $sliderClass = 'mosaic';
         $thumbDimensions = array("w" => 155, "h" => 100);
     } else {
         if ($orientation == 'right') {
             $sliderClass = 'vertical';
             $thumbDimensions = array("w" => 110, "h" => 50);
         } else {
             $sliderClass = 'horizontal';
             $thumbDimensions = array("w" => 90, "h" => 70);
         }
     }
     /* temp transition code until grid is fully rolled out, remove and integrate after transition */
     if ($orientation == 'mosaic' && !empty($wgOasisGrid)) {
         $thumbDimensions['w'] = WikiaPhotoGalleryHelper::WIKIA_GRID_THUMBNAIL_MAX_WIDTH;
     }
     /* end temp transistion code */
     $imageServingForThumbs = new ImageServing(null, $thumbDimensions['w'], $thumbDimensions);
     $out = array();
     $sliderImageLimit = $orientation == 'mosaic' ? 5 : 4;
     foreach ($this->mImages as $p => $pair) {
         /**
          * @var $nt Title
          * @var $text String
          * @var $link String
          */
         $nt = $pair[0];
         $text = $pair[1];
         $link = $pair[2];
         $linkText = $this->mData['images'][$p]['linktext'];
         $shortText = $this->mData['images'][$p]['shorttext'];
         $time = $descQuery = false;
         // parse link (RT #142515)
         $linkAttribs = $this->parseLink($nt->getLocalUrl(), $nt->getText(), $link);
         wfRunHooks('BeforeGalleryFindFile', array(&$this, &$nt, &$time, &$descQuery));
         $img = wfFindFile($nt, $time);
         if (!WikiaFileHelper::isFileTypeVideo($img) && is_object($img) && $nt->getNamespace() == NS_FILE) {
             if (F::app()->checkSkin('wikiamobile')) {
                 $imageUrl = wfReplaceImageServer($img->getUrl(), $img->getTimestamp());
             } else {
                 // generate cropped version of big image (fit within 660x360 box)
                 // BugId:9678 image thumbnailer does not always land on 360px height since we scale on width
                 // so this also scales image UP if it is too small (stretched is better than blank)
                 // max() added due to BugId:20644
                 $imageUrl = $imageServingForImages->getUrl($img, max($imagesDimensions['w'], $img->getWidth()), max($imagesDimensions['h'], $img->getHeight()));
             }
             // generate navigation thumbnails
             $thumbUrl = $imageServingForThumbs->getUrl($img, $img->getWidth(), $img->getHeight());
             $data = array('imageUrl' => $imageUrl, 'imageTitle' => $text, 'imageShortTitle' => $shortText, 'imageLink' => !empty($link) ? $linkAttribs['href'] : '', 'imageDescription' => $linkText, 'imageThumbnail' => $thumbUrl);
             if (F::app()->checkSkin('wikiamobile')) {
                 $origWidth = $img->getWidth();
                 $origHeight = $img->getHeight();
                 $size = WikiaMobileMediaService::calculateMediaSize($origWidth, $origHeight);
                 $thumb = $img->transform($size);
                 $imageAttribs = array('src' => wfReplaceImageServer($thumb->getUrl(), $img->getTimestamp()), 'width' => $size['width'], 'height' => $size['height']);
                 $imageParams = array('full' => $imageUrl);
                 $data['mediaInfo'] = array('attributes' => $imageAttribs, 'parameters' => $imageParams, 'caption' => $text, 'noscript' => Xml::element('img', $imageAttribs, '', true));
             }
             $out[] = $data;
         }
         if (count($out) >= $sliderImageLimit) {
             break;
         }
     }
     $html = '';
     //check if we have something to show (images might not match required sizes)
     if (count($out)) {
         $template = new EasyTemplate(dirname(__FILE__) . '/templates');
         $template->set_vars(array('sliderClass' => $sliderClass, 'images' => $out, 'thumbDimensions' => $thumbDimensions, 'sliderId' => $this->mData['id'], 'imagesDimensions' => $imagesDimensions));
         if (F::app()->checkSkin('wikiamobile')) {
             $html = $template->render('renderWikiaMobileSlider');
         } else {
             if ($orientation == 'mosaic') {
                 $html = $template->render('renderMosaicSlider');
             } else {
                 $html = $template->render('renderSlider');
             }
         }
         if ($orientation == 'mosaic') {
             $sliderResources = array('/resources/wikia/libraries/modernizr/modernizr-2.0.6.js', '/extensions/wikia/WikiaPhotoGallery/css/WikiaPhotoGallery.slidertag.mosaic.scss', '/extensions/wikia/WikiaPhotoGallery/js/WikiaPhotoGallery.slider.mosaic.js');
             $javascriptInitializationFunction = 'WikiaMosaicSliderMasterControl.init';
         } else {
             $sliderResources = array('/extensions/wikia/WikiaPhotoGallery/css/WikiaPhotoGallery.slidertag.css', '/extensions/wikia/WikiaPhotoGallery/js/WikiaPhotoGallery.slider.js');
             $javascriptInitializationFunction = 'WikiaPhotoGallerySlider.init';
         }
         $html .= F::build('JSSnippets')->addToStack($sliderResources, array(), $javascriptInitializationFunction, array($this->mData['id']));
         //load WikiaMobile resources if needed using JSSnippets filtering mechanism
         $html .= F::build('JSSnippets')->addToStack(array('wikiaphotogallery_slider_scss_wikiamobile', 'wikiaphotogallery_slider_js_wikiamobile'));
     }
     wfProfileOut(__METHOD__);
     return $html;
 }