Brian Cherdak
Brian Cherdak

Reputation: 109

uploader convert any format image into jpg image

working on an uploader and added a switch statement to it to convert any image .png, .gif, .bmp, into a jpg.

But it doesn't seem to work, is anyone able to explain me the issue of why it passes through the switch and stil keeps the file format as .gif or png?

<?php

if (isset($_POST['addpart']))
    {
    $image = $_FILES['images']['tmp_name'];
    $name = $_POST['username'];
    $i = 0;
    foreach($image as $key)
        {
        $fileData = pathinfo(basename($_FILES["images"]["name"][$i]));
        $fileName[] = $name . '_' . uniqid() . '.' . $fileData['extension'];
        move_uploaded_file($key, "image/" . end($fileName));
        copy("image/" . end($fileName) , "image_thumbnail/" . end($fileName));
        $images = "image_thumbnail/" . end($fileName);
        $new_images = "image_thumbnail/" . end($fileName);
        $width = 100; //*** Fix Width & Heigh (Autu caculate) ***//
        $size = GetimageSize($images);
        $height = round($width * $size[1] / $size[0]);

        switch ($size[2])
            {
        case IMAGETYPE_GIF:
            $images_orig = imagecreatefromgif($images);
            break;

        case IMAGETYPE_JPEG:
            $images_orig = imagecreatefromjpeg($images);
            break;

        case IMAGETYPE_PNG:
            $images_orig = imagecreatefrompng($images);
            break;

        default:
            die("Unknown filetype");
            }

        //$images_orig = imagecreatefromjpeg($images);
        $photoX = ImagesX($images_orig);
        $photoY = ImagesY($images_orig);
        $images_fin = ImageCreateTrueColor($width, $height);
        ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
        ImageJPEG($images_fin, $new_images);
        $i++;
        }

    echo 'Uploaded<br />';
    echo 'Main Image - ' . $fileName[0] . '<br />';
    echo 'Extra Image 1 - ' . $fileName[1] . '<br />';
    echo 'Extra Image 2 - ' . $fileName[2] . '<br />';
    echo '<hr>';
    }

?>

Upvotes: 0

Views: 767

Answers (1)

Brian Cherdak
Brian Cherdak

Reputation: 109

Thanks for all the assistance that everyone gave on this uploader.

The features are

  • compact code
  • unique file identifier
  • numbers files with a +1 increment, starting with 1 as the first file
  • can add a word infront of it, for example a session username or a
    first name
  • can add as many file boxes as needed
  • converts images to jpg in both the original format and new thumb version.
  • thumb can set a max width and the height becomes proportional so it doesn't become distorted
  • 2 different directories to separate thumb and original
  • can grab image data so it can be inserted into a database very easily.

Here is the final code, it is a useful snippet for any body who wants to rip it apart.

<?php
if (isset($_POST['addpart'])) {
    $image = $_FILES['images']['tmp_name'];
    $name  = $_POST['username'];
    $i     = 0;
    $i2    = 1;
    $id    = uniqid();
    foreach ($image as $key) {
        $fileData   = pathinfo(basename($_FILES["images"]["name"][$i]));
        $fileName[] = $name . '_' . $id . '_' . $i2 . '.' . $fileData['name'] . 'jpg';
        move_uploaded_file($key, "image/" . end($fileName));
        $images     = "image/" . end($fileName);
        $new_images = "image_thumbnail/" . end($fileName);
        $width      = 100; //*** Fix Width & Heigh (Autu caculate) ***//
        $size       = GetimageSize($images);
        $height     = round($width * $size[1] / $size[0]);

        switch ($size[2]) {
            case IMAGETYPE_GIF:
                $images_orig = imagecreatefromgif($images);
                break;

            case IMAGETYPE_JPEG:
                $images_orig = imagecreatefromjpeg($images);
                break;

            case IMAGETYPE_PNG:
                $images_orig = imagecreatefrompng($images);
                break;

            default:
                die("Unknown filetype");
        }

        //$images_orig = imagecreatefromjpeg($images);
        $photoX     = ImagesX($images_orig);
        $photoY     = ImagesY($images_orig);
        $images_fin = ImageCreateTrueColor($width, $height);
        ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
        ImageJPEG($images_fin, $new_images);
        $i++;
        $i2++;
    }

    echo 'Uploaded<br />';
    echo 'Main Image - ' . $fileName[0] . '<br />';
    echo 'Extra Image 1 - ' . $fileName[1] . '<br />';
    echo 'Extra Image 2 - ' . $fileName[2] . '<br />';
    echo '<hr>';
}

?>
<form action="" method="post" enctype="multipart/form-data" id="form">
username - <input name="username" type="text" id="username" size="50" /><br>
Main Image - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
Extra Image 1 - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
Extra Image 2 - <input name="images[]" type="file" id="images[]" size="50" accept="image/jpeg" /><br>
<input type="submit" name="addpart" value="Upload" /><br>
</form>

Upvotes: 1

Related Questions