Rated an average of 0 stars by 0 people

Uploader with Thumbnail

Script Details:
Added on June 12, 2015 by chrisshaw. Last modified June 12, 2015.
chrisshaw accreditted the script to "Christopher Shaw : http://www.chris-shaw.com."

Description:
A quick script that uploads an image to a set directory then creates a thumbnail in a separate directory before presenting the link to both.

Tags: php, thumb, upload

Instructions:
Requires PHP and Imagick.
Upload directories need write access

Code:

<?php
# Snippet from PHP Share: http://www.phpshare.org

/*
Title: Image Uploader And Thumb
Purpose: To create a thumbnail
for an uploaded file and placing in correct directories . Author: Christopher Shaw
Website: http:// www . chris - shaw . com
Had to change format due to phpshare . org formatter

*/

$thumb_path './img/gallery/thumbs/';
$full_path './img/gallery/';

class 
Image
{
    public static function 
thumbnail($file$destination$width$height)
    {
        
$im = new imagick($file);
        
$im->cropThumbnailImage($width$height);
        
$im->writeImage($destination);
    }
    
    public static function 
upload($file$destination)
    {
        
$ext pathinfo($destination);
        
$ext strtolower($ext['extension']);
        
        if (!
in_array($ext, array('png''jpg''jpeg''gif')))
        {
            return 
false;
        }
        
        
move_uploaded_file($file$destination);
        return 
true;
    }
}

$output null;

if (isset(
$_FILES['file']['name']))
{
    
$name $_FILES['file']['name'];
    
$tmp_name $_FILES['file']['tmp_name'];
    
    if (
Image:: upload($tmp_name$full_path $name))
    {
        
Image:: thumbnail($full_path $name$thumb_path $name200200);
        
        
$output .= 'Full:< a href = "'$full_path 'Full: <a href="' $name '' $full_path '">' $name '</ a >< br /> ';
        
$output .= 'Thumb:< a href = "'$thumb_path '' $name '</a><br />' $thumb_path 'Thumb: <a href="' $name '</ a >< br /> ';
    }
}

?>
<form  method="post" enctype="multipart/form-data">
        <fieldset>
        <legend>Image Uploader</legend>
            File: <input type="file" name="file"><br />
            <input type="submit" value="Upload Image" name="submit">
    </fieldset>
</form>
<?= $output 
?>

Review this PHP Script:

Rating:

Comments:


Login

User:
Pass:

Register

Not a member? Register today and receive the many free benefits of being a member!

Advertisements