Jump to content

Image upload / resize


tryingtolearn

Recommended Posts

I am looking for a little help on an image upload and reize script.
This beast started out just to upload 1 .jpg file
Resize it to two different sizes - add the name to a MYsql database and spit out the results.

Then evolved - I needed to do .gif also
Then evolved once more to handle .png.

That all worked (Allthough I think it is way more complicated then it needs to be but it works)
Now I would like to be able to upload resize and record all those types to the database but instead of doing 1 at a time on the form I would like to do 5 at a time.

Can this existing code be worked to do that?
I was totally confused getting it to this point so I apologize if it is a mess to get through.

I understood the whole array idea for multiple uploads but couldnt get anything to like that to do the resizing of the individual pics???

[code]
<?php

if(isset($_POST['Submit']))

{
require_once ('../mysql_connect.php'); // Connect to the database.
    $user_id=$_SESSION['user_id'];
    
    

    $size = 300; // the thumbnail height
    $size2 = 150;
    $filedir = 'images/'; // the directory for the original image
    $thumbdir = 'images/t/'; // the directory for the thumbnail image
    $listdir = 'images/tl/'; // the directory for the thumbnail image
    $prefix = 'small_'; // the prefix to be added to the original name

    $maxfile = '10000000';
    $mode = '0777';
    
    $userfile_name = $_FILES['image']['name'];
    $userfile_tmp = $_FILES['image']['tmp_name'];
    $userfile_size = $_FILES['image']['size'];
    $userfile_type = $_FILES['image']['type'];
    if (!($userfile_type =="image/pjpeg" OR $userfile_type =="image/jpeg" OR $userfile_type =="image/jpg" OR $userfile_type =="image/gif" OR $userfile_type =="image/png" OR $userfile_type =="image/x-png")) {
    die ("You can only load .jpg, .gif, or .png images.  Please <a href=\"photogal.php\">try again</a>.");
}

// Check for terms.
    if (!empty($userfile_name)) {
        $i = escape_data($userfile_name);
    
    
            // Make the query.
$query = "INSERT INTO img (user_id, img_name) VALUES ('$user_id', '$i')";
            $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
            if (mysql_affected_rows() == 1)
    
        
        
        mysql_close(); // Close the database connection.

     // End of the main Submit conditional.
     }
if ($userfile_type =="image/pjpeg" OR $userfile_type =="image/jpeg" OR $userfile_type =="image/jpg") {
    if (isset($_FILES['image']['name']))
    {
    
    
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }else{
            $new_height = $size;
            $new_width = abs($new_height/$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving');
        imagedestroy($destimg);
        
        $listthumb = $listdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size2)
        {
            $new_width2 = $sizes[0];
            $new_height2 = $sizes[1];
        }else{
            $new_height2 = $size2;
            $new_width2 = abs($new_height2/$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width2,$new_height2) or die('Problem In Creating image');
        $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image');
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width2,$new_height2,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageJPEG($destimg,$listthumb,90) or die('Problem In saving');
        imagedestroy($destimg);
    }

    echo 'Image uploaded!<br><a href="photogal.php">Upload another image</a><br>
    <a href="'.$prod_img.'" target="_blank">
        <img src="'.$prod_img_thumb.'" width="'.$new_width.'" height="'.$new_height.'" border="0">
    </a><br><br>
    <a href="'.$prod_img.'"target="_blank">
        <img src="'.$listthumb.'" width="'.$new_width2.'" height="'.$new_height2.'" border="0">
    </a>
    ';
}
if ($userfile_type =="image/png" OR $userfile_type =="image/x-png") {
    if (isset($_FILES['image']['name']))
    {
    
    
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }else{
            $new_height = $size;
            $new_width = abs($new_height/$aspect_ratio);
        }



        $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromPNG($prod_img) or die('Problem In opening Source Image');
        $colourBlack = imagecolorallocate($destimg, 0, 0, 0);
        imagecolortransparent($destimg, $colourBlack);
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImagePNG($destimg,$prod_img_thumb) or die('Problem In saving');
        imagedestroy($destimg);
        
        $listthumb = $listdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size2)
        {
            $new_width2 = $sizes[0];
            $new_height2 = $sizes[1];
        }else{
            $new_height2 = $size2;
            $new_width2 = abs($new_height2/$aspect_ratio);
        }

        $destimg=ImageCreateTrueColor($new_width2,$new_height2) or die('Problem In Creating image');
        $srcimg=ImageCreateFromPNG($prod_img) or die('Problem In opening Source Image');
        $colourBlack = imagecolorallocate($destimg, 0, 0, 0);
        imagecolortransparent($destimg, $colourBlack);
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width2,$new_height2,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImagePNG($destimg,$listthumb) or die('Problem In saving');
        imagedestroy($destimg);
    }

    echo 'Image uploaded!<br><a href="photogal.php">Upload another image</a><br>
    <a href="'.$prod_img.'" target="_blank">
        <img src="'.$prod_img_thumb.'" width="'.$new_width.'" height="'.$new_height.'" border="0">
    </a><br><br>
    <a href="'.$prod_img.'"target="_blank">
        <img src="'.$listthumb.'" width="'.$new_width2.'" height="'.$new_height2.'" border="0">
    </a>
    ';
}



if ($userfile_type =="image/gif") {
    if (isset($_FILES['image']['name']))
    {
    
    
        $prod_img = $filedir.$userfile_name;

        $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size)
        {
            $new_width = $sizes[0];
            $new_height = $sizes[1];
        }else{
            $new_height = $size;
            $new_width = abs($new_height/$aspect_ratio);
        }



        $destimg=ImageCreate($new_width,$new_height) or die('Problem In Creating image');
        $srcimg=ImageCreateFromGIF($prod_img) or die('Problem In opening Source Image');
        $colourBlack = imagecolorallocate($destimg, 0, 0, 0);
        imagecolortransparent($destimg, $colourBlack);
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageGIF($destimg,$prod_img_thumb) or die('Problem In saving');
        imagedestroy($destimg);
        
        $listthumb = $listdir.$prefix.$userfile_name;
        move_uploaded_file($userfile_tmp, $prod_img);
        chmod ($prod_img, octdec($mode));
        
        $sizes = getimagesize($prod_img);

        $aspect_ratio = $sizes[1]/$sizes[0];

        if ($sizes[1] <= $size2)
        {
            $new_width2 = $sizes[0];
            $new_height2 = $sizes[1];
        }else{
            $new_height2 = $size2;
            $new_width2 = abs($new_height2/$aspect_ratio);
        }

        $destimg=ImageCreate($new_width2,$new_height2) or die('Problem In Creating image');
        $srcimg=ImageCreateFromGIF($prod_img) or die('Problem In opening Source Image');
        $colourBlack = imagecolorallocate($destimg, 255, 255, 255);
        imagecolortransparent($destimg, $colourBlack);
        ImageCopyResized($destimg,$srcimg,0,0,0,0,$new_width2,$new_height2,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing');
        ImageGIF($destimg,$listthumb) or die('Problem In saving');
        imagedestroy($destimg);
    }

    echo 'Image uploaded!<br><a href="photogal.php">Upload another image</a><br>
    <a href="'.$prod_img.'" target="_blank">
        <img src="'.$prod_img_thumb.'" width="'.$new_width.'" height="'.$new_height.'" border="0">
    </a><br><br>
    <a href="'.$prod_img.'"target="_blank">
        <img src="'.$listthumb.'" width="'.$new_width2.'" height="'.$new_height2.'" border="0">
    </a>
    ';
}

}

else{

    echo '
    <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
    <p><input type="file" name="image"></p>    
    <input type="Submit" name="Submit" value="Submit">
    </form>';
}

?>
[/code]

I would appreciate any direction anyone can provide.
Link to comment
https://forums.phpfreaks.com/topic/8199-image-upload-resize/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.