Forum Moderators: coopster

Message Too Old, No Replies

Customizing Image Upload Script

         

matthewamzn

9:50 pm on May 1, 2006 (gmt 0)

10+ Year Member



I'm trying to create a second set of thumbnails for my website. This is the attachment part of the script. How can I duplicate the thumbnail generation.

// SET VARS
$is_error = "";
$general_error = "";
$upload_error = "";
$image_url = "http://";

if($task == "attach_do") {

$image_url = $_POST['image_url'];
$file_name = $_FILES['image_file']['name'];
$file_type = $_FILES['image_file']['type'];
$file_size = $_FILES['image_file']['size'];
$file_tempname = $_FILES['image_file']['tmp_name'];
$file_error = $_FILES['image_file']['error'];

$allow_images = "yes";
if($group_info[allow_uploads] == "1") {
$extensions = $group_info[uploads_extensions];
if(mysql_num_rows(mysql_query("SELECT f_id FROM bhost_folders WHERE u_id='$user_info[u_id]' AND folder='Uploaded Images'"))!= 1) {
mysql_query("INSERT INTO bhost_folders (u_id, in_album, folder, description) VALUES ('$user_info[u_id]', '0', '$attach8', '$attach9')");
}
$folder_info = mysql_fetch_assoc(mysql_query("SELECT f_id FROM bhost_folders WHERE u_id='$user_info[u_id]' AND folder='$attach8'"));
$f_id = $folder_info[f_id];
$location = "folder";
} elseif($group_info[allow_album] == "1") {
$extensions = $group_info[album_extensions];
$f_id = 0;
$location = "album";
} else {
$allow_images = "no";
}

if(str_replace("http://", "", str_replace(" ", "", $image_url)) == "" & str_replace(" ", "", $file_name) == "") {
$is_error = "yes";
$general_error = "$attach10";
}

if(str_replace("http://", "", str_replace(" ", "", $image_url))!= "" & str_replace(" ", "", $file_name)!= "") {
$is_error = "yes";
$general_error = "$attach11";
}

if(isset($file_name) & str_replace(" ", "", $file_name)!= "" & $allow_images == "yes" & $is_error!= "yes") {
$file_name = str_replace(" ", "_", $file_name);
$file_name = str_replace("?", "", $file_name);
$file_name = str_replace("&", "", $file_name);
$file_name = str_replace("=", "", $file_name);
$file_name = str_replace("#", "", $file_name);
$file_name = str_replace("%", "", $file_name);
$file_name = str_replace("'", "", $file_name);
$file_name = str_replace("\"", "", $file_name);
$file_name = stripslashes($file_name);

$ext = strtolower(strrchr($file_name, "."));
$ext = str_replace(".", "", $ext);

$extlist = str_replace(" ", "", $extensions);
$extlist = str_replace(",", ", ", $extlist);
$extensions_case = strtolower($extensions);
$extensions = explode(",", $extensions_case);

if(!in_array($ext, $extensions)) {
$is_error = "yes";
$upload_error = "$attach12";
}

if($is_error!= "yes") {
$type = strtolower($file_type);
if($ext == "jpg" OR $ext == "jpeg" OR $ext == "jpe") {
if($type!= "image/jpeg" AND $type!= "image/jpg" AND $type!= "image/jpe" AND $type!= "image/pjpeg" AND $type!= "image/pjpg" AND $type!= "image/x-jpeg" AND $type!= "image/x-jpg") {
$is_error = "yes";
$upload_error = "$attach12";
}}
elseif($ext == "gif") {
if($type!= "image/gif" AND $type!= "image/x-gif") {
$is_error = "yes";
$upload_error = "$attach12";
}}
elseif($ext == "png") {
if($type!= "image/png" AND $type!= "image/x-png") {
$is_error = "yes";
$upload_error = "$attach12";
}}
elseif($ext == "tiff") {
if($type!= "image/tif" AND $type!= "image/tiff") {
$is_error = "yes";
$upload_error = "$attach12";
}}
elseif($ext == "bmp") {
if($type!= "image/bmp" AND $type!= "image/x-bmp") {
$is_error = "yes";
$upload_error = "$attach12";
}}
}

if($is_error!= "yes") {
$space_allowed = $group_info[uploads_space] * 1024;
$files = mysql_fetch_assoc(mysql_query("SELECT sum(filesize) AS size FROM bhost_uploads WHERE u_id='$user_info[u_id]'"));
$space_used = $files[size]+$file_size;

if($space_used > $space_allowed) {
$is_error = "yes";
$upload_error = "$attach13";
}
}

if($is_error!= "yes") {
$dimension = @getimagesize($file_tempname);
if($dimension[0]!= "" AND $dimension[1]!= "") { $dimensions = "$dimension[0]x$dimension[1]"; }
mysql_query("INSERT INTO bhost_uploads (u_id, f_id, location, ext, filesize, name, description, dimensions) VALUES ('$user_info[u_id]', '$f_id', '$location', '$ext', '$file_size', '$file_name', '', '$dimensions')") or die(mysql_error());
$image_info = mysql_fetch_assoc(mysql_query("SELECT * FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND f_id='$f_id' AND location='$location' AND filesize='$file_size' AND name='$file_name' AND description='' ORDER BY up_id DESC LIMIT 1"));

$newfilename = "$image_info[up_id].$ext";
$letter = strtolower(substr($user_info[username], 0, 1));
$path = "../uploads/$letter/$user_info[username]/$newfilename";
$image_url = str_replace("manager/attach.php", "", $_SERVER['PHP_SELF']);
$image_url = "http://".$_SERVER['HTTP_HOST'].$image_url."uploads/$letter/$user_info[username]/$image_info[up_id].$ext";

if(!move_uploaded_file($file_tempname, $path)) {
mysql_query("DELETE FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND f_id='$f_id' AND location='$location' AND filesize='$file_size' AND name='$name' AND description='$description'");
$is_error = "yes";
$upload_error = "$attach14";
} else {

//CREATE THUMBNAILS

// GET FILE DIMENSIONS
$dimension = explode("x", $image_info[dimensions]);
$width = $dimension[0];
$height = $dimension[1];

// MAKE THUMBNAILS
while($width > 150 OR $height > 150) {
$width = $width * .95;
$height = $height * .95;
}
$width = (int)$width;
$height = (int)$height;

if(function_exists('gd_info')) {
$thumbpath = "../uploads/$letter/$user_info[username]/$image_info[up_id]"."_thumb.$ext";
if($ext == "gif") {
$thumb = imagecreatetruecolor($width, $height);
$new = imagecreatefromgif("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
for($i=0; $i<256; $i++) {
imagecolorallocate($thumb, $i, $i, $i);
}
$size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($thumb, $thumbpath);
ImageDestroy($new);
ImageDestroy($thumb);
} elseif($ext == "bmp") {
$thumb = imagecreatetruecolor($width, $height);
$new = imagecreatefrombmp("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
for($i=0; $i<256; $i++) {
imagecolorallocate($thumb, $i, $i, $i);
}
$size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($thumb, $thumbpath);
ImageDestroy($new);
ImageDestroy($thumb);
} elseif($ext == "jpeg" ¦ $ext == "jpg") {
$thumb = imagecreatetruecolor($width, $height);
$new = imagecreatefromjpeg("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
for($i=0; $i<256; $i++) {
imagecolorallocate($thumb, $i, $i, $i);
}
$size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagejpeg($thumb, $thumbpath);
ImageDestroy($new);
ImageDestroy($thumb);
} elseif($ext == "png") {
$thumb = imagecreatetruecolor($width, $height);
$new = imagecreatefrompng("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
for($i=0; $i<256; $i++) {
imagecolorallocate($thumb, $i, $i, $i);
}
$size = getimagesize("../uploads/$letter/$user_info[username]/$image_info[up_id].$ext");
imagecopyresampled($thumb, $new, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagepng($thumb, $thumbpath);
ImageDestroy($new);
ImageDestroy($thumb);
}
}

@chmod($path, 0777);
}
}
}

jatar_k

10:06 pm on May 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



to be honest, that is a ton of code and I'm not going to read through it all

did you just want to reuse existing code for thumbnail generation in another part of the script?

if so maybe move it into a function and then you can call that function everywhere it is needed