所以我在這個任務上創建了一個適度靈活的,但最重要的是,可重複使用的處理PHP腳本的圖像上傳項目。當我繼續前進時,我碰到了一個PHP內存限制問題,我在stackoverflow上發佈了這個問題(可以在這裏找到:PHP Memory Limit),我得到的真棒和有用的答案讓我意識到我基本上在優化我的PHP腳本。我認爲我會發布我目前用作上載腳本的「可重用」PHP表單處理程序,並歡迎任何反饋,因爲那裏的智能開發人員可能會提高性能或全面改善它。提高PHP圖像上傳/調整大小腳本的性能
綜上所述什麼該處理程序應該做的:
1)允許圖像被上傳
2)除被調整爲期望的寬度
3)所述圖像的全尺寸版本保存的縮略圖大小的版本將尺寸調整爲所需寬度的圖像
4)在兩個圖像上放置水印。
我使用兩個開源腳本來幫助調整大小和水印。我如何有效地使用它們我並不積極,但它們工作並且非常友好。
Simple Image PHP Script:
http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
Zubrak's Thumbnail Script:
http://www.zubrag.com/scripts/watermark-image.php
這裏是我的處理程序:
<?php
// If a file is being uploaded, do somethin' about it!:
if (!empty($_FILES)) {
// CONFIGURE:
// How many pixels wide should the full size image be?
$fullSizeWidth = 800;
// How many pixels wide should the thumbnail image be?
$thumbnailWidth = 100;
// What is the path to the image upload directory?
$pathToImageDirectory = "path/to/image/directory/";
// Create an array of allowable extension types:
$validExtensions = array('jpg', 'jpeg', 'png');
// What will the thumbnail version's suffix be?
$thumbnailSuffix = "_thumbnail";
// What is the path to your watermark image file?
$pathToWatermark = "path/to/watermark/watermark.png";
// INCLUDE NEEDED FILES
// Require the simpleImage class for basic image modifications
require_once('simpleImage.php');
// Require the Zubrag_watermark class for adding your watermark to images
require_once('Zubrag_watermark.php');
// GET THE USER DATA FROM THE FORM (for demo we'll just say they're submitting an image file only):
// Get the file's temporary name:
$tempFile = $_FILES['file']['tmp_name'];
// Get the file's original name:
$userFileName = $_FILES['file']['name'];
// Get the file's extension:
$extension = strtolower(end(explode(".", $userFileName)));
// UPLOAD DESITNATION:
// Re-name the image something cool (We'll just hash it for now):
$theImageName = sha1($userFileName);
// Create the full sized image destination by combining it all
$imageDestination = $pathToImageDirectory . $theImageName . "." . $extension;
// Create the thumbnail sized image destination by combining it all
$thumbnailDestination = $pathToImageDirectory . $theImageName . $thumbnailSuffix . "." . $extension;
// VALIDATE THE IMAGE:
// Check to see if the uploaded file has an acceptable extension
if(in_array($extension, $validExtensions)) {
$validExtension = true;
} else {
$validExtension = false;
}
// Run getImageSize function to check that we're really getting an image
if(getimagesize($tempFile) == false) {
$validImage = false;
} else {
$validImage = true;
}
// If the extension is valid and the image is valid, accept the file, resize it, and watermark it:
if($validExtension == true && $validImage == true) {
if(move_uploaded_file($tempFile,$imageDestination)) {
// RESIZE THE IMAGES
// Create simpleImage object
$image = new SimpleImage();
// Load the uploaded file to memory
$image->load($imageDestination);
// Resize the image to desired full size width
$image->resizeToWidth($fullSizeWidth);
// Save the image's full sized version
$image->save($imageDestination);
// Resize the image to the desired thumbnail width
$image->resizeToWidth($thumbnailWidth);
// Save the image's thumbnail sized version
$image->save($thumbnailDestination);
// Free the image from memory (note: I added this function to the simpleImage class -- it's simply: imagedestroy($this->image);)
$image->Free();
// WATERMARK THE IMAGES
// Load the full size image into memory
$watermark = new Zubrag_watermark($imageDestination);
// Apply the watermark
$watermark->ApplyWatermark($pathToWatermark);
// Save the watermarked full-sized file
$watermark->SaveAsFile($imageDestination);
// Free the full sized image from memory
$watermark->Free();
// Load the thumbnail sized image into memory
$watermark = new Zubrag_watermark($thumbnailDestination);
// Apply the watermark
$watermark->ApplyWatermark($pathToWatermark);
// Save the thumbnail-sized File
$watermark->SaveAsFile($thumbnailDestination);
// Free the image from memory
$watermark->Free();
}
} else {
// Error handling for an image that did not pass validation
echo "So we're basically thinking you tried to upload something that wasn't an image.";
}
} else {
// Error handling for running this script without a file being uploaded
echo "You should probably upload a file next time.";
}
感謝所有...任何幫助/思念/辯論/反饋將非常感激。
所以這個問題真的是......「可能你,人們,配對編程這個?」 – ZJR 2010-09-25 04:21:30
考慮這個問題的答案:http://stackoverflow.com/questions/12661/efficient-jpeg-image-resizing -in-PHP/4613341#4613341 – 2011-01-10 08:31:41