我首先描述这段代码,它的作用
/**
* Stores any image uploaded from the edit form
*
* @param assoc The 'image' element from the $_FILES array containing the file upload data
*/
public function storeUploadedImage( $image ) {
if ( $image['error'] == UPLOAD_ERR_OK )
{
// Does the Article object have an ID?
if ( is_null( $this->id ) ) trigger_error( "Article::storeUploadedImage(): Attempt to upload an image for an Article object that does not have its ID property set.", E_USER_ERROR );
// Delete any previous image(s) for this article
$this->deleteImages();
// Get and store the image filename extension
$this->imageExtension = strtolower( strrchr( $image['name'], '.' ) );
// Store the image
$tempFilename = trim( $image['tmp_name'] );
if ( is_uploaded_file ( $tempFilename ) ) {
if ( !( move_uploaded_file( $tempFilename, $this->getImagePath() ) ) ) trigger_error( "Article::storeUploadedImage(): Couldn't move uploaded file.", E_USER_ERROR );
if ( !( chmod( $this->getImagePath(), 0666 ) ) ) trigger_error( "Article::storeUploadedImage(): Couldn't set permissions on uploaded file.", E_USER_ERROR );
}
// Get the image size and type
$attrs = getimagesize ( $this->getImagePath() );
$imageWidth = $attrs[0];
$imageHeight = $attrs[1];
$imageType = $attrs[2];
// Load the image into memory
switch ( $imageType ) {
case IMAGETYPE_GIF:
$imageResource = imagecreatefromgif ( $this->getImagePath() );
break;
case IMAGETYPE_JPEG:
$imageResource = imagecreatefromjpeg ( $this->getImagePath() );
break;
case IMAGETYPE_PNG:
$imageResource = imagecreatefrompng ( $this->getImagePath() );
break;
default:
trigger_error ( "Article::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR );
}
// Copy and resize the image to create the thumbnail
$thumbHeight = intval ( $imageHeight / $imageWidth * ARTICLE_THUMB_WIDTH );
$thumbResource = imagecreatetruecolor ( ARTICLE_THUMB_WIDTH, $thumbHeight );
imagecopyresampled( $thumbResource, $imageResource, 0, 0, 0, 0, ARTICLE_THUMB_WIDTH, $thumbHeight, $imageWidth, $imageHeight );
// Save the thumbnail
switch ( $imageType ) {
case IMAGETYPE_GIF:
imagegif ( $thumbResource, $this->getImagePath( IMG_TYPE_THUMB ) );
break;
case IMAGETYPE_JPEG:
imagejpeg ( $thumbResource, $this->getImagePath( IMG_TYPE_THUMB ), JPEG_QUALITY );
break;
case IMAGETYPE_PNG:
imagepng ( $thumbResource, $this->getImagePath( IMG_TYPE_THUMB ) );
break;
default:
trigger_error ( "Article::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR );
}
$this->update();
}
}
/**
* Deletes any images and/or thumbnails associated with the article
*/
public function deleteImages() {
// Delete all fullsize images for this article
foreach (glob( ARTICLE_IMAGE_PATH . "/" . IMG_TYPE_FULLSIZE . "/" . $this->id . ".*") as $filename) {
if ( !unlink( $filename ) ) trigger_error( "Article::deleteImages(): Couldn't delete image file.", E_USER_ERROR );
}
// Delete all thumbnail images for this article
foreach (glob( ARTICLE_IMAGE_PATH . "/" . IMG_TYPE_THUMB . "/" . $this->id . ".*") as $filename) {
if ( !unlink( $filename ) ) trigger_error( "Article::deleteImages(): Couldn't delete thumbnail file.", E_USER_ERROR );
}
// Remove the image filename extension from the object
$this->imageExtension = "";
}
/**
* Returns the relative path to the article's full-size or thumbnail image
*
* @param string The type of image path to retrieve (IMG_TYPE_FULLSIZE or IMG_TYPE_THUMB). Defaults to IMG_TYPE_FULLSIZE.
* @return string|false The image's path, or false if an image hasn't been uploaded
*/
public function getImagePath( $type=IMG_TYPE_FULLSIZE ) {
return ( $this->id && $this->imageExtension ) ? ( ARTICLE_IMAGE_PATH . "/$type/" . $this->id . $this->imageExtension ) : false;
}
- 每篇文章上传一张图片和一张缩略图
- 重命名图片(例如,如果数据库文章id为5,则重命名后的全尺寸和缩略图为5)
我想修改一个 CMS,它只允许每篇文章上传一张图片。所以我的所有意图是每篇文章上传 5 张图片。
而我的第二个意图是根据文章标题重命名每个文件(例如,如果文章标题是诺基亚N9,则上传时的第一张图片将是nokia_n9_1.jpg,第二张将是nokia_n9_2.jpg,左侧也为第 1 和第 2。
我是 PHP OOP 的新手。我知道有很多工作要做。帮助将不胜感激。如果您想查看完整的 CMS 源代码,请查看链接
http://www.elated.com/articles/cms-in-an-afternoon-php-mysql/
http://www.elated.com/articles/add-image-uploading-to-your-cms/