Example #1
0
 /**
  * Create a thumbnail of the image having the specified width.
  * The thumbnail will not be created if the width is larger than the
  * image's width. Let the browser do the scaling in this case.
  * The thumbnail is stored on disk and is only computed if the thumbnail
  * file does not exist OR if it is older than the image.
  * Returns an object which can return the pathname, URL, and physical
  * pixel size of the thumbnail -- or null on failure.
  *
  * @return ThumbnailImage or null on failure
  * @private
  */
 function renderThumb($width, $useScript = true, $makeMapIcon = false)
 {
     // WERELATE: makeMapIcon
     global $wgUseSquid, $wgThumbnailEpoch;
     wfProfileIn(__METHOD__);
     $this->load();
     $height = -1;
     if (!$this->validateThumbParams($width, $height)) {
         # Validation error
         wfProfileOut(__METHOD__);
         return null;
     }
     // WERELATE: makeMapIcon
     if (!$this->mustRender() && $width == $this->width && $height == $this->height && !$makeMapIcon) {
         # validateThumbParams (or the user) wants us to return the unscaled image
         $thumb = new ThumbnailImage($this->getURL(), $width, $height);
         wfProfileOut(__METHOD__);
         return $thumb;
     }
     // WERELATE: makeMapIcon
     list($isScriptUrl, $url) = $this->thumbUrl($width, 'thumb', $makeMapIcon);
     if ($isScriptUrl && $useScript) {
         // Use thumb.php to render the image
         $thumb = new ThumbnailImage($url, $width, $height);
         wfProfileOut(__METHOD__);
         return $thumb;
     }
     // WERELATE: makeMapIcon
     $thumbName = $this->thumbName($width, $makeMapIcon);
     $thumbDir = wfImageThumbDir($this->name, $this->fromSharedDirectory);
     $thumbPath = $thumbDir . '/' . $thumbName;
     if (is_dir($thumbPath)) {
         // Directory where file should be
         // This happened occasionally due to broken migration code in 1.5
         // Rename to broken-*
         global $wgUploadDirectory;
         for ($i = 0; $i < 100; $i++) {
             $broken = "{$wgUploadDirectory}/broken-{$i}-{$thumbName}";
             if (!file_exists($broken)) {
                 rename($thumbPath, $broken);
                 break;
             }
         }
         // Code below will ask if it exists, and the answer is now no
         clearstatcache();
     }
     $done = true;
     if (!file_exists($thumbPath) || filemtime($thumbPath) < wfTimestamp(TS_UNIX, $wgThumbnailEpoch)) {
         // Create the directory if it doesn't exist
         if (is_file($thumbDir)) {
             // File where thumb directory should be, destroy if possible
             @unlink($thumbDir);
         }
         wfMkdirParents($thumbDir);
         $oldThumbPath = wfDeprecatedThumbDir($thumbName, 'thumb', $this->fromSharedDirectory) . '/' . $thumbName;
         $done = false;
         // Migration from old directory structure
         if (is_file($oldThumbPath)) {
             if (filemtime($oldThumbPath) >= filemtime($this->imagePath)) {
                 if (file_exists($thumbPath)) {
                     if (!is_dir($thumbPath)) {
                         // Old image in the way of rename
                         unlink($thumbPath);
                     } else {
                         // This should have been dealt with already
                         throw new MWException("Directory where image should be: {$thumbPath}");
                     }
                 }
                 // Rename the old image into the new location
                 rename($oldThumbPath, $thumbPath);
                 $done = true;
             } else {
                 unlink($oldThumbPath);
             }
         }
         if (!$done) {
             // WERELATE: makeMapIcon
             $this->lastError = $this->reallyRenderThumb($thumbPath, $width, $height, $makeMapIcon);
             if ($this->lastError === true) {
                 $done = true;
             } elseif ($GLOBALS['wgIgnoreImageErrors']) {
                 // Log the error but output anyway.
                 // With luck it's a transitory error...
                 $done = true;
             }
             # Purge squid
             # This has to be done after the image is updated and present for all machines on NFS,
             # or else the old version might be stored into the squid again
             if ($wgUseSquid) {
                 $urlArr = array($url);
                 wfPurgeSquidServers($urlArr);
             }
         }
     }
     if ($done) {
         $thumb = new ThumbnailImage($url, $width, $height, $thumbPath);
     } else {
         $thumb = null;
     }
     wfProfileOut(__METHOD__);
     return $thumb;
 }
 /**
  * Create a thumbnail of the image having the specified width.
  * The thumbnail will not be created if the width is larger than the
  * image's width. Let the browser do the scaling in this case.
  * The thumbnail is stored on disk and is only computed if the thumbnail
  * file does not exist OR if it is older than the image.
  * Returns an object which can return the pathname, URL, and physical
  * pixel size of the thumbnail -- or null on failure.
  *
  * @return ThumbnailImage
  * @access private
  */
 function renderThumb($width, $useScript = true)
 {
     global $wgUseSquid, $wgInternalServer;
     global $wgThumbnailScriptPath, $wgSharedThumbnailScriptPath;
     $fname = 'Image::renderThumb';
     wfProfileIn($fname);
     $width = IntVal($width);
     $this->load();
     if (!$this->exists()) {
         # If there is no image, there will be no thumbnail
         wfProfileOut($fname);
         return null;
     }
     # Sanity check $width
     if ($width <= 0 || $this->width <= 0) {
         # BZZZT
         wfProfileOut($fname);
         return null;
     }
     global $wgSVGMaxSize;
     $maxsize = $this->mustRender() ? max($this->width, $wgSVGMaxSize) : $this->width - 1;
     if ($width > $maxsize) {
         # Don't make an image bigger than the source
         $thumb = new ThumbnailImage($this->getViewURL(), $this->getWidth(), $this->getHeight());
         wfProfileOut($fname);
         return $thumb;
     }
     $height = floor($this->height * ($width / $this->width));
     list($isScriptUrl, $url) = $this->thumbUrl($width);
     if ($isScriptUrl && $useScript) {
         // Use thumb.php to render the image
         $thumb = new ThumbnailImage($url, $width, $height);
         wfProfileOut($fname);
         return $thumb;
     }
     $thumbName = $this->thumbName($width, $this->fromSharedDirectory);
     $thumbPath = wfImageThumbDir($this->name, $this->fromSharedDirectory) . '/' . $thumbName;
     if (is_dir($thumbPath)) {
         // Directory where file should be
         // This happened occasionally due to broken migration code in 1.5
         // Rename to broken-*
         global $wgUploadDirectory;
         for ($i = 0; $i < 100; $i++) {
             $broken = "{$wgUploadDirectory}/broken-{$i}-{$thumbName}";
             if (!file_exists($broken)) {
                 rename($thumbPath, $broken);
                 break;
             }
         }
         // Code below will ask if it exists, and the answer is now no
         clearstatcache();
     }
     if (!file_exists($thumbPath)) {
         $oldThumbPath = wfDeprecatedThumbDir($thumbName, 'thumb', $this->fromSharedDirectory) . '/' . $thumbName;
         $done = false;
         // Migration from old directory structure
         if (is_file($oldThumbPath)) {
             if (filemtime($oldThumbPath) >= filemtime($this->imagePath)) {
                 if (file_exists($thumbPath)) {
                     if (!is_dir($thumbPath)) {
                         // Old image in the way of rename
                         unlink($thumbPath);
                     } else {
                         // This should have been dealt with already
                         wfDebugDieBacktrace("Directory where image should be: {$thumbPath}");
                     }
                 }
                 // Rename the old image into the new location
                 rename($oldThumbPath, $thumbPath);
                 $done = true;
             } else {
                 unlink($oldThumbPath);
             }
         }
         if (!$done) {
             $this->reallyRenderThumb($thumbPath, $width, $height);
             # Purge squid
             # This has to be done after the image is updated and present for all machines on NFS,
             # or else the old version might be stored into the squid again
             if ($wgUseSquid) {
                 if (substr($url, 0, 4) == 'http') {
                     $urlArr = array($url);
                 } else {
                     $urlArr = array($wgInternalServer . $url);
                 }
                 wfPurgeSquidServers($urlArr);
             }
         }
     }
     $thumb = new ThumbnailImage($url, $width, $height, $thumbPath);
     wfProfileOut($fname);
     return $thumb;
 }
Example #3
0
 /**
  * Create a thumbnail of the image having the specified width.
  * The thumbnail will not be created if the width is larger than the
  * image's width. Let the browser do the scaling in this case.
  * The thumbnail is stored on disk and is only computed if the thumbnail
  * file does not exist OR if it is older than the image.
  * Returns an object which can return the pathname, URL, and physical
  * pixel size of the thumbnail -- or null on failure.
  *
  * @return ThumbnailImage
  * @access private
  */
 function renderThumb($width, $useScript = true)
 {
     global $wgUseSquid, $wgInternalServer;
     global $wgThumbnailScriptPath, $wgSharedThumbnailScriptPath;
     $fname = 'Image::renderThumb';
     wfProfileIn($fname);
     $width = IntVal($width);
     $this->load();
     if (!$this->exists()) {
         # If there is no image, there will be no thumbnail
         wfProfileOut($fname);
         return null;
     }
     # Sanity check $width
     if ($width <= 0 || $this->width <= 0) {
         # BZZZT
         wfProfileOut($fname);
         return null;
     }
     if ($width >= $this->width && !$this->mustRender()) {
         # Don't make an image bigger than the source
         $thumb = new ThumbnailImage($this->getViewURL(), $this->getWidth(), $this->getHeight());
         wfProfileOut($fname);
         return $thumb;
     }
     $height = floor($this->height * ($width / $this->width));
     list($isScriptUrl, $url) = $this->thumbUrl($width);
     if ($isScriptUrl && $useScript) {
         // Use thumb.php to render the image
         $thumb = new ThumbnailImage($url, $width, $height);
         wfProfileOut($fname);
         return $thumb;
     }
     $thumbName = $this->thumbName($width, $this->fromSharedDirectory);
     $thumbPath = wfImageThumbDir($this->name, $this->fromSharedDirectory) . '/' . $thumbName;
     if (!file_exists($thumbPath)) {
         $oldThumbPath = wfDeprecatedThumbDir($thumbName, 'thumb', $this->fromSharedDirectory) . '/' . $thumbName;
         $done = false;
         if (file_exists($oldThumbPath)) {
             if (filemtime($oldThumbPath) >= filemtime($this->imagePath)) {
                 rename($oldThumbPath, $thumbPath);
                 $done = true;
             } else {
                 unlink($oldThumbPath);
             }
         }
         if (!$done) {
             $this->reallyRenderThumb($thumbPath, $width, $height);
             # Purge squid
             # This has to be done after the image is updated and present for all machines on NFS,
             # or else the old version might be stored into the squid again
             if ($wgUseSquid) {
                 if (substr($url, 0, 4) == 'http') {
                     $urlArr = array($url);
                 } else {
                     $urlArr = array($wgInternalServer . $url);
                 }
                 wfPurgeSquidServers($urlArr);
             }
         }
     }
     $thumb = new ThumbnailImage($url, $width, $height, $thumbPath);
     wfProfileOut($fname);
     return $thumb;
 }