Jump to content

resizing images during upload


allaboutthekick

Recommended Posts

I would like to impliment a image resizer into this script that takes the image if it is bigger then 450w and resizes it. I dont know where or how to insert the code into this script, please suggest anything, thank you.

[code]
<?php // define the base image dir

include("logincheck.php");

$base_img_dir = "./img/";

// define location of image conversion programs
$img_conv_dir = "./bin/";

// define database table containing image info
$img_table = "images";

// connect with database
include('dbcon.php');


// generate unique id for use in filename
$uniq = uniqid("");

// new file name
$filename = $base_img_dir.$uniq;

// move uploaded file to destination
move_uploaded_file($HTTP_POST_FILES["file"]["tmp_name"], $filename);

// retrieve image info
$imginfo = getimagesize($filename);

// handle image according to type
switch ($imginfo[2]) {
    case 1: // gif
        // convert gif to png using shell command
        $command = $img_conv_dir."gif2png $filename";
        exec($command);
       
        // remove original gif file and rename converted png
        unlink($filename);
        rename("$filename.png", $filename);
       
        // check png image by loading and saving the file
        //  to prevent wrong uploaded files and errors
        $img = imagecreatefrompng($filename);
        imagepng($img, $filename);
        imagedestroy($img);
       
        // set image type to png
        $img_type = "PNG";
        break;
case 2: // jpeg
        // check jpeg image by loading and saving the file
        //  to prevent wrong uploaded files and errors
        $img = imagecreatefromjpeg($filename);
        imagejpeg($img, $filename);
        imagedestroy($img);
       
        // set image type to jpeg
        $img_type = "JPG";
        break;

    case 3: // png
        // check png image by loading and saving the file
        //  to prevent wrong uploaded files and errors
        $img = imagecreatefrompng($filename);
        imagepng($img, $filename);
        imagedestroy($img);
       
        // set image type to png
        $img_type = "PNG";
        break;
case 4: // bmp
        // rename file to bmp
        rename($filename, "$filename.bmp");
       
        // convert bmp to png using shell command
        $command = $img_conv_dir."bmptoppm $filename.bmp | ".
                  $img_conv_dir."pnmtopng > $filename";
        exec($command);
       
        // remove original bmp
        unlink("$filename.bmp");
       
        // check png image by loading and saving the file
        //  to prevent wrong uploaded files and errors
        $img = imagecreatefrompng($filename);
        imagepng($img, $filename);
        imagedestroy($img);
       
        // set image type to png
        $img_type = "PNG";
        break;

    default:
        break;
}
// retrieve image file size
$imgbytes = filesize($filename);
// insert image into db
mysql_query("INSERT INTO $img_table (username, img_file, img_type, img_height,
  img_width, img_bytes, img_title, img_descr, img_alt)
  VALUES( '$username', '$uniq', '$img_type', ".$imginfo[1].", ".$imginfo[0].",
        $imgbytes, '".addslashes($HTTP_POST_VARS["title"])."', '".
        addslashes($HTTP_POST_VARS["descr"])."',
        '".addslashes($HTTP_POST_VARS["alt"])."');");

// display some information
echo "Image uploaded.<br><img src=\"img/$uniq\"><br>".
    "URL: img/$uniq";

?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/33818-resizing-images-during-upload/
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.