Click to See Complete Forum and Search --> : Improving code??


Perfidus
11-19-2003, 11:17 AM
This is the terrible way I have "copy-pasted-developed" a code to upload some pictures, rename them and create thumbnail. As you will see is pure rubbish, but it works...
There should be a way to do it more trendy by calling a function, but this is too much for me (and this too: <? echo "I'm a nerd newbie"; ?> ) but trendyness let me sleep quite well so, the problems are:
1 How to check if they are jpg or not.
2 How to allow user uploading only 1 or 2 instead all.

The code dustman





<?
$msg = "";
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {

case true:

if(!isset($_FILES['image']) || $_FILES['image'] == "none" || $_FILES['image'] == "") {
$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Seleccione sus fotografías pulsando -Examinar-</font>";
break;
}

$tmp = getcwd()."/".$_FILES['image']['name'];

if(!@move_uploaded_file($_FILES['image']['tmp_nam
e'], $tmp)) {

$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Ha ocurrido un error al subir la imagen</font>";
break;
}

$fp = fopen($tmp, "rb");
$str = fread($fp, filesize($tmp));

fclose($fp);
unlink($tmp);

$im1 = ImageCreateFromString($str);

$imgname = $numref."thumb_1"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth = 150; // Ancho máximo
$maxheight = 150; // Alto máximo

$width1 = ImageSX($im1);
$height1 = ImageSY($im1);
$width2 = $maxwidth;
$height2 = floor(($width2 * $height1) / $width1);

if($maxheight > 0 && $height2 > $maxheight) {

$height2 = $maxheight;
$width2 = floor(($height2 * $width1) / $height1);
}

$im2 = ImageCreateTrueColor($width2, $height2);
ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

ImageJpeg($im2, "thumb/".$imgname.".jpg"); // Se copia en temp/
$msg = "Ok";
$im3 = ImageCreateFromString($str);

$imgname2 = $numref."_1"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth2 = 500; // Ancho máximo
$maxheight2 = 300; // Alto máximo

$width12 = ImageSX($im3);
$height12 = ImageSY($im3);
$width22 = $maxwidth2;
$height22 = floor(($width22 * $height12) / $width12);

if($maxheight2 > 0 && $height22 > $maxheight2) {

$height22 = $maxheight2;
$width22 = floor(($height22 * $width12) / $height12);
}

$im4 = ImageCreateTrueColor($width22, $height22);
ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

ImageJpeg($im4, "full/".$imgname2.".jpg"); // Se copia en temp/

break;
}
$msg = "";
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {

case true:

if(!isset($_FILES['image2']) || $_FILES['image2'] == "none" || $_FILES['image2'] == "") {
$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Seleccione sus fotografías pulsando -Examinar-</font>";
break;
}

$tmp = getcwd()."/".$_FILES['image2']['name'];

if(!@move_uploaded_file($_FILES['image2']['tmp_na
me'], $tmp)) {

$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Ha ocurrido un error al subir la imagen</font>";
break;
}

$fp = fopen($tmp, "rb");
$str = fread($fp, filesize($tmp));

fclose($fp);
unlink($tmp);

$im1 = ImageCreateFromString($str);

$imgname = $numref."thumb_2"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth = 150; // Ancho máximo
$maxheight = 150; // Alto máximo

$width1 = ImageSX($im1);
$height1 = ImageSY($im1);
$width2 = $maxwidth;
$height2 = floor(($width2 * $height1) / $width1);

if($maxheight > 0 && $height2 > $maxheight) {

$height2 = $maxheight;
$width2 = floor(($height2 * $width1) / $height1);
}

$im2 = ImageCreateTrueColor($width2, $height2);
ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

ImageJpeg($im2, "thumb/".$imgname.".jpg"); // Se copia en temp/
$msg = "Ok";
$im3 = ImageCreateFromString($str);

$imgname2 = $numref."_2"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth2 = 500; // Ancho máximo
$maxheight2 = 300; // Alto máximo

$width12 = ImageSX($im3);
$height12 = ImageSY($im3);
$width22 = $maxwidth2;
$height22 = floor(($width22 * $height12) / $width12);

if($maxheight2 > 0 && $height22 > $maxheight2) {

$height22 = $maxheight2;
$width22 = floor(($height22 * $width12) / $height12);
}

$im4 = ImageCreateTrueColor($width22, $height22);
ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

ImageJpeg($im4, "full/".$imgname2.".jpg"); // Se copia en temp/


break;
}
$msg = "";
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {

case true:

if(!isset($_FILES['image3']) || $_FILES['image3'] == "none" || $_FILES['image3'] == "") {
$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Seleccione sus fotografías pulsando -Examinar-</font>";
break;
}

$tmp = getcwd()."/".$_FILES['image3']['name'];

if(!@move_uploaded_file($_FILES['image3']['tmp_na
me'], $tmp)) {

$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Ha ocurrido un error al subir la imagen</font>";
break;
}

$fp = fopen($tmp, "rb");
$str = fread($fp, filesize($tmp));

fclose($fp);
unlink($tmp);

$im1 = ImageCreateFromString($str);

$imgname = $numref."thumb_3"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth = 150; // Ancho máximo
$maxheight = 150; // Alto máximo

$width1 = ImageSX($im1);
$height1 = ImageSY($im1);
$width2 = $maxwidth;
$height2 = floor(($width2 * $height1) / $width1);

if($maxheight > 0 && $height2 > $maxheight) {

$height2 = $maxheight;
$width2 = floor(($height2 * $width1) / $height1);
}

$im2 = ImageCreateTrueColor($width2, $height2);
ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

ImageJpeg($im2, "thumb/".$imgname.".jpg"); // Se copia en temp/
$msg = "Ok";
$im3 = ImageCreateFromString($str);

$imgname2 = $numref."_3"; // Nombre imagen (se podría indicar desde el formulario)
$maxwidth2 = 500; // Ancho máximo
$maxheight2 = 300; // Alto máximo

$width12 = ImageSX($im3);
$height12 = ImageSY($im3);
$width22 = $maxwidth2;
$height22 = floor(($width22 * $height12) / $width12);

if($maxheight2 > 0 && $height22 > $maxheight2) {

$height22 = $maxheight2;
$width22 = floor(($height22 * $width12) / $height12);
}

$im4 = ImageCreateTrueColor($width22, $height22);
ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

ImageJpeg($im4, "full/".$imgname2.".jpg"); // Se copia en temp/
ImageDestroy($im1);
ImageDestroy($im2);
ImageDestroy($im3);
ImageDestroy($im4);
$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>La imagen ha sido cargada con éxito</font>";

break;
}
?>