Search code examples
phpfile-uploaduploadifyimage-uploading

File uploaded, but thumbnail not created in modified Uploadify script


I have a fairly simple script that handles the file information sent to the server from Uploadify and everything works except the creation of the thumbnail. Can anyone see where my mistake is?

<?php
session_start();
include "../_db.inc";

/*
Uploadify v3.1.0
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php> 
*/

// Define a destination
$targetFolder = '/images/artist_pictures/'; // Relative to the root
$thumbsFolder = '/images/artist_pictures/thumbs/'; // Relative to the root

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
        $yourid = (int)$_POST['yourid'];
        $extension = end(explode(".", $_FILES['Filedata']['name']));
        $filename = "artist_".$yourid.".".$extension;
    $targetFile = rtrim($targetPath,'/') . '/' . $filename;
        $targetThumb = rtrim($thumbsFolder,'/') . '/' . $filename;

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png','JPG','bmp'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
                // CREATE THUMBNAIL
                if($extension=="jpg" || $extension=="jpeg") {
                    $src = imagecreatefromjpeg($tempFile);
                }
                else if($extension=="png") {
                    $src = imagecreatefrompng($tempFile);
                }
                else {
                    $src = imagecreatefromgif($tempFile);
                }

                list($width,$height)=getimagesize($tempFile);

                $newwidth=50;
                $newheight=($height/$width)*$newwidth;
                $tmp=imagecreatetruecolor($newwidth,$newheight);

                imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

                $thumbname = $targetThumb;

                if (file_exists($thumbname)) {
                    unlink($thumbname);
        }

                imagejpeg($tmp,$thumbname,100);

                imagedestroy($src);
                imagedestroy($tmp);

        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}
?>

Thanks in advance! Colin


Solution

  • I tested your code and it works with a few changes of cause

    Used

    $targetFolder = realpath('/temp'); // Relative to the root
    $thumbsFolder = realpath('/temp/thumbs/'); // Relative to the root
    

    There is also an error at $extension = end(explode(".", $_FILES['Filedata']['name']));

    Replace it to

      $extension = $fileParts ['extension'];
    

    Full Code

    <?php
    session_start ();
    //include "../_db.inc";
    
    /*
     * Uploadify v3.1.0 Copyright (c) 2012 Reactive Apps, Ronnie Garcia Released
     * under the MIT License <http://www.opensource.org/licenses/mit-license.php>
     */
    
    // Define a destination
        $targetFolder = realpath('/temp'); // Relative to the root
        $thumbsFolder = realpath('/temp/thumbs/'); // Relative to the root
    
    if (! empty ( $_FILES )) {
        $tempFile = $_FILES ['Filedata'] ['tmp_name'];
        $targetPath = $_SERVER ['DOCUMENT_ROOT'] . $targetFolder;
        $yourid = 2 ;//( int ) $_POST ['yourid'];
    
        $fileParts = pathinfo ( $_FILES ['Filedata'] ['name'] );
        $extension = $fileParts ['extension'];
        $filename = "artist_" . $yourid . "." . $fileParts ['extension'];
        $targetFile = rtrim ( $targetPath, '/' ) . '/' . $filename;
        $targetThumb = rtrim ( $thumbsFolder, '/' ) . '/' . $filename;
    
        // Validate the file type
        $fileTypes = array (
                'jpg',
                'jpeg',
                'gif',
                'png',
                'JPG',
                'bmp' 
        ); // File extensions
    
        if (in_array ( $fileParts ['extension'], $fileTypes )) {
            // CREATE THUMBNAIL
            if ($extension == "jpg" || $extension == "jpeg") {
                $src = imagecreatefromjpeg ( $tempFile );
            } else if ($extension == "png") {
                $src = imagecreatefrompng ( $tempFile );
            } else {
                $src = imagecreatefromgif ( $tempFile );
            }
    
            list ( $width, $height ) = getimagesize ( $tempFile );
    
            $newwidth = 50;
            $newheight = ($height / $width) * $newwidth;
            $tmp = imagecreatetruecolor ( $newwidth, $newheight );
    
            imagecopyresampled ( $tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height );
    
            $thumbname = $targetThumb;
    
            if (file_exists ( $thumbname )) {
                unlink ( $thumbname );
            }
    
            imagejpeg ( $tmp, $thumbname, 100 );
    
            imagedestroy ( $src );
            imagedestroy ( $tmp );
    
            move_uploaded_file ( $tempFile, $targetFile );
            echo '1';
        } else {
            echo 'Invalid file type.';
        }
    }
    ?>
    
    
    <form method="post" target="_self" enctype="multipart/form-data">
        <input type="file" name="Filedata" id="file" /> <input name="submit"
            type="submit" value="submit" />
    </form>
    

    Conclusion .. this issue seem to be your file path and some php undefined errors yse the folloing to output the errors

     error_reporting(E_ALL);
     ini_set('display_errors','On');