提升PHP图像上传/调整大小脚本的性能

时间:2010-09-25 04:07:40

标签: php security image optimization file-upload

所以我正在执行这项任务,为图像上传项目创建一个适度灵活但最重要的可重用处理程序PHP脚本。随着我的巡航,我遇到了一个我在stackoverflow上发布的PHP内存限制问题(可以在这里找到:PHP Memory Limit),我得到的令人敬畏和有用的答案让我意识到我基本上都在优化我的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.";
}

谢谢大家......非常感谢任何帮助/想法/辩论/反馈。

1 个答案:

答案 0 :(得分:0)

可能完全不同的解决方案可能是在发送之前尝试在客户端中执行此操作。我只用它来管理上传,但如果你查看http://plupload.com,他们就有一个很棒的上传工具。它实际上会在照片发送之前在客户端(flash或html5)上进行一些调整。您和用户的上传时间更快,上传的用户体验也非常出色。