Bir PHP resim yüklenme Arttırılması performans / komut dosyası yeniden boyutlandırma

0 Cevap php

Yani bir orta esnek oluşturmak için bu misyonu vardı, ama resim yükleme projeler için en önemlisi, yeniden kullanılabilir işleyici PHP betiği. Ben StackOverflow yayınlanan bir PHP bellek limiti söz koştu birlikte ben cruised (burada bulunabilir: http://stackoverflow.com/questions/3792058/php-memory-limit) ve bana ben temelde benim PHP optimize emmek fark yapılan var harika ve yararlı cevaplar komut. Ben anda yükleme komut benim 'yeniden kullanılabilir' PHP form işleyici olarak var ve orada akıllı Devs performansını artırmak veya çevresinde onu geliştirmek zorunda kalabilirsiniz görüşlerinizi bekliyoruz ne yayınlamak düşündüm.

To sum what this handler should do:
1) Allow images to be uploaded
2) Save a full size version of the image that is resized to a desired width
3) Save a thumbnail sized version of the image that is resized to a desired width
4) Place a watermark on both images.

Ben yeniden boyutlandırma ve filigran ile yardımcı olmak için iki açık kaynak komut kullanıyorum. Ben bunları kullanıyorum nasıl verimli Ben hakkında olumlu değilim, ama onlar samimi oldukça kullanıcı çalışmaya ve vardır.

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

İşte benim işleyicisi var:

<?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.";
}

Teşekkürler tüm ... Herhangi bir yardım / düşünce / tartışma / geribildirim gerçekten takdir.

0 Cevap