image.php
字号:
$dbw =& wfGetDB( DB_MASTER, 'commons' ); $dbw->selectDB( $wgSharedUploadDBname ); } else { $dbw =& wfGetDB( DB_MASTER ); } $this->checkDBSchema($dbw); list( $major, $minor ) = self::splitMime( $this->mime ); wfDebug(__METHOD__.': upgrading '.$this->name." to 1.5 schema\n"); $dbw->update( 'image', array( 'img_width' => $this->width, 'img_height' => $this->height, 'img_bits' => $this->bits, 'img_media_type' => $this->type, 'img_major_mime' => $major, 'img_minor_mime' => $minor, 'img_metadata' => $this->metadata, ), array( 'img_name' => $this->name ), __METHOD__ ); if ( $this->fromSharedDirectory ) { $dbw->selectDB( $wgDBname ); } wfProfileOut( __METHOD__ ); } /** * Split an internet media type into its two components; if not * a two-part name, set the minor type to 'unknown'. * * @param $mime "text/html" etc * @return array ("text", "html") etc */ static function splitMime( $mime ) { if( strpos( $mime, '/' ) !== false ) { return explode( '/', $mime, 2 ); } else { return array( $mime, 'unknown' ); } } /** * Return the name of this image * @public */ function getName() { return $this->name; } /** * Return the associated title object * @public */ function getTitle() { return $this->title; } /** * Return the URL of the image file * @public */ function getURL() { if ( !$this->url ) { $this->load(); if($this->fileExists) { $this->url = Image::imageUrl( $this->name, $this->fromSharedDirectory ); } else { $this->url = ''; } } return $this->url; } function getViewURL() { if( $this->mustRender()) { if( $this->canRender() ) { return $this->createThumb( $this->getWidth() ); } else { wfDebug('Image::getViewURL(): supposed to render '.$this->name.' ('.$this->mime."), but can't!\n"); return $this->getURL(); #hm... return NULL? } } else { return $this->getURL(); } } /** * Return the image path of the image in the * local file system as an absolute path * @public */ function getImagePath() { $this->load(); return $this->imagePath; } /** * Return the width of the image * * Returns -1 if the file specified is not a known image type * @public */ function getWidth() { $this->load(); return $this->width; } /** * Return the height of the image * * Returns -1 if the file specified is not a known image type * @public */ function getHeight() { $this->load(); return $this->height; } /** * Return the size of the image file, in bytes * @public */ function getSize() { $this->load(); return $this->size; } /** * Returns the mime type of the file. */ function getMimeType() { $this->load(); return $this->mime; } /** * Return the type of the media in the file. * Use the value returned by this function with the MEDIATYPE_xxx constants. */ function getMediaType() { $this->load(); return $this->type; } /** * Checks if the file can be presented to the browser as a bitmap. * * Currently, this checks if the file is an image format * that can be converted to a format * supported by all browsers (namely GIF, PNG and JPEG), * or if it is an SVG image and SVG conversion is enabled. * * @todo remember the result of this check. */ function canRender() { global $wgUseImageMagick; if( $this->getWidth()<=0 || $this->getHeight()<=0 ) return false; $mime= $this->getMimeType(); if (!$mime || $mime==='unknown' || $mime==='unknown/unknown') return false; #if it's SVG, check if there's a converter enabled if ($mime === 'image/svg') { global $wgSVGConverters, $wgSVGConverter; if ($wgSVGConverter && isset( $wgSVGConverters[$wgSVGConverter])) { wfDebug( "Image::canRender: SVG is ready!\n" ); return true; } else { wfDebug( "Image::canRender: SVG renderer missing\n" ); } } #image formats available on ALL browsers if ( $mime === 'image/gif' || $mime === 'image/png' || $mime === 'image/jpeg' ) return true; #image formats that can be converted to the above formats if ($wgUseImageMagick) { #convertable by ImageMagick (there are more...) if ( $mime === 'image/vnd.wap.wbmp' || $mime === 'image/x-xbitmap' || $mime === 'image/x-xpixmap' #|| $mime === 'image/x-icon' #file may be split into multiple parts || $mime === 'image/x-portable-anymap' || $mime === 'image/x-portable-bitmap' || $mime === 'image/x-portable-graymap' || $mime === 'image/x-portable-pixmap' #|| $mime === 'image/x-photoshop' #this takes a lot of CPU and RAM! || $mime === 'image/x-rgb' || $mime === 'image/x-bmp' || $mime === 'image/tiff' ) return true; } else { #convertable by the PHP GD image lib if ( $mime === 'image/vnd.wap.wbmp' || $mime === 'image/x-xbitmap' ) return true; } return false; } /** * Return true if the file is of a type that can't be directly * rendered by typical browsers and needs to be re-rasterized. * * This returns true for everything but the bitmap types * supported by all browsers, i.e. JPEG; GIF and PNG. It will * also return true for any non-image formats. * * @return bool */ function mustRender() { $mime= $this->getMimeType(); if ( $mime === "image/gif" || $mime === "image/png" || $mime === "image/jpeg" ) return false; return true; } /** * Determines if this media file may be shown inline on a page. * * This is currently synonymous to canRender(), but this could be * extended to also allow inline display of other media, * like flash animations or videos. If you do so, please keep in mind that * that could be a security risk. */ function allowInlineDisplay() { return $this->canRender(); } /** * Determines if this media file is in a format that is unlikely to * contain viruses or malicious content. It uses the global * $wgTrustedMediaFormats list to determine if the file is safe. * * This is used to show a warning on the description page of non-safe files. * It may also be used to disallow direct [[media:...]] links to such files. * * Note that this function will always return true if allowInlineDisplay() * or isTrustedFile() is true for this file. */ function isSafeFile() { if ($this->allowInlineDisplay()) return true; if ($this->isTrustedFile()) return true; global $wgTrustedMediaFormats; $type= $this->getMediaType(); $mime= $this->getMimeType(); #wfDebug("Image::isSafeFile: type= $type, mime= $mime\n"); if (!$type || $type===MEDIATYPE_UNKNOWN) return false; #unknown type, not trusted if ( in_array( $type, $wgTrustedMediaFormats) ) return true; if ($mime==="unknown/unknown") return false; #unknown type, not trusted if ( in_array( $mime, $wgTrustedMediaFormats) ) return true; return false; } /** Returns true if the file is flagged as trusted. Files flagged that way * can be linked to directly, even if that is not allowed for this type of * file normally. * * This is a dummy function right now and always returns false. It could be * implemented to extract a flag from the database. The trusted flag could be * set on upload, if the user has sufficient privileges, to bypass script- * and html-filters. It may even be coupled with cryptographics signatures * or such. */ function isTrustedFile() { #this could be implemented to check a flag in the databas, #look for signatures, etc return false; } /** * Return the escapeLocalURL of this image * @public */ function getEscapeLocalURL() { $this->getTitle(); return $this->title->escapeLocalURL(); } /** * Return the escapeFullURL of this image * @public */ function getEscapeFullURL() { $this->getTitle(); return $this->title->escapeFullURL(); } /** * Return the URL of an image, provided its name. * * @param string $name Name of the image, without the leading "Image:" * @param boolean $fromSharedDirectory Should this be in $wgSharedUploadPath? * @return string URL of $name image * @public * @static */ function imageUrl( $name, $fromSharedDirectory = false ) { global $wgUploadPath,$wgUploadBaseUrl,$wgSharedUploadPath; if($fromSharedDirectory) { $base = ''; $path = $wgSharedUploadPath; } else { $base = $wgUploadBaseUrl; $path = $wgUploadPath; } $url = "{$base}{$path}" . wfGetHashPath($name, $fromSharedDirectory) . "{$name}"; return wfUrlencode( $url ); } /** * Returns true if the image file exists on disk. * @return boolean Whether image file exist on disk. * @public */ function exists() { $this->load(); return $this->fileExists; } /** * @todo document * @private */ function thumbUrl( $width, $subdir='thumb') { global $wgUploadPath, $wgUploadBaseUrl, $wgSharedUploadPath; global $wgSharedThumbnailScriptPath, $wgThumbnailScriptPath; // Generate thumb.php URL if possible $script = false; $url = false; if ( $this->fromSharedDirectory ) { if ( $wgSharedThumbnailScriptPath ) { $script = $wgSharedThumbnailScriptPath; } } else { if ( $wgThumbnailScriptPath ) { $script = $wgThumbnailScriptPath; } } if ( $script ) { $url = $script . '?f=' . urlencode( $this->name ) . '&w=' . urlencode( $width ); if( $this->mustRender() ) { $url.= '&r=1'; } } else { $name = $this->thumbName( $width ); if($this->fromSharedDirectory) { $base = ''; $path = $wgSharedUploadPath; } else { $base = $wgUploadBaseUrl; $path = $wgUploadPath; } if ( Image::isHashed( $this->fromSharedDirectory ) ) { $url = "{$base}{$path}/{$subdir}" . wfGetHashPath($this->name, $this->fromSharedDirectory) . $this->name.'/'.$name; $url = wfUrlencode( $url ); } else { $url = "{$base}{$path}/{$subdir}/{$name}"; } } return array( $script !== false, $url ); } /** * Return the file name of a thumbnail of the specified width * * @param integer $width Width of the thumbnail image * @param boolean $shared Does the thumbnail come from the shared repository? * @private */ function thumbName( $width ) { $thumb = $width."px-".$this->name; if( $this->mustRender() ) { if( $this->canRender() ) { # Rasterize to PNG (for SVG vector images, etc) $thumb .= '.png'; } else { #should we use iconThumb here to get a symbolic thumbnail? #or should we fail with an internal error? return NULL; //can't make bitmap } } return $thumb; } /** * Create a thumbnail of the image having the specified width/height. * 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 the URL. * * Keeps aspect ratio of original image. If both width and height are * specified, the generated image will be no bigger than width x height, * and will also have correct aspect ratio. * * @param integer $width maximum width of the generated thumbnail * @param integer $height maximum height of the image (optional) * @public */ function createThumb( $width, $height=-1 ) { $thumb = $this->getThumbnail( $width, $height ); if( is_null( $thumb ) ) return ''; return $thumb->getUrl(); } /** * As createThumb, but returns a ThumbnailImage object. This can * provide access to the actual file, the real size of the thumb, * and can produce a convenient <img> tag for you. * * For non-image formats, this may return a filetype-specific icon. * * @param integer $width maximum width of the generated thumbnail * @param integer $height maximum height of the image (optional) * @param boolean $render True to render the thumbnail if it doesn't exist, * false to just return the URL * * @return ThumbnailImage or null on failure
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -