2010-09-25 127 views
0

所以我在这个任务上创建了一个适度灵活的,但最重要的是,可重复使用的处理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."; 
} 

感谢所有...任何帮助/思念/辩论/反馈将非常感激。

+0

所以这个问题真的是......“可能你,人们,配对编程这个?” – ZJR 2010-09-25 04:21:30

+0

考虑这个问题的答案:http://stackoverflow.com/questions/12661/efficient-jpeg-image-resizing -in-PHP/4613341#4613341 – 2011-01-10 08:31:41

回答

0

一个可能完全不同的解决方案可能是在发送之前在客户端尝试这样做。我只使用它来管理上传,但如果您检出http://plupload.com,则它们具有很好的上传实用程序。在照片发送之前,它实际上会对客户端进行一些调整大小(使用flash或html5)。您和用户的上传速度更快,而且上传的用户体验也非常出色。