Page 1 of 1

Upload, resize, and add to db an image

Posted: Fri Nov 18, 2005 2:46 pm
by waradmin
I want to have a form to allow people to upload a picture into a 'profile'. Upon uploading the picture i want the max dimensions to be at the max 150px by 150px but not to make the image distorted. Then lastly i need the uploaded files location, and dimensions to be put into a database so they can be recalled later.

If someone can help with a upload form that does resizing as well as puting the L and W into a database along with the uploaded file location.

Thanks in advance.

Posted: Fri Nov 18, 2005 2:57 pm
by seodevhead
Hi. I am looking to find out how to do the exact same thing with a script of mine. Please, if you find out how to do this, please post your findings here so I can learn as well. Thanks :)

Posted: Fri Nov 18, 2005 4:38 pm
by Ambush Commander

Code: Select all

<?php

//NEEDS GD
//Our resident image resize function, is faster than the given
//SELF-SUFFICIENT
function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
  $scaleX = ($src_w - 1) / $dst_w;
  $scaleY = ($src_h - 1) / $dst_h;

  $scaleX2 = $scaleX / 2.0;
  $scaleY2 = $scaleY / 2.0;

  $tc = imageistruecolor($src_img);

  for ($y = $src_y; $y < $src_y + $dst_h; $y++)
  {
   $sY  = $y * $scaleY;
   $siY  = (int) $sY;
   $siY2 = (int) $sY + $scaleY2;

   for ($x = $src_x; $x < $src_x + $dst_w; $x++)
   {
     $sX  = $x * $scaleX;
     $siX  = (int) $sX;
     $siX2 = (int) $sX + $scaleX2;

     if ($tc)
     {
       $c1 = imagecolorat($src_img, $siX, $siY2);
       $c2 = imagecolorat($src_img, $siX, $siY);
       $c3 = imagecolorat($src_img, $siX2, $siY2);
       $c4 = imagecolorat($src_img, $siX2, $siY);

       $r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
       $g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
       $b = ((($c1 & 0xFF)  + ($c2 & 0xFF)  + ($c3 & 0xFF)  + ($c4 & 0xFF))  >> 2);

       imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
     }
     else
     {
       $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
       $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
       $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
       $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));

       $r = ($c1['red']  + $c2['red']  + $c3['red']  + $c4['red']  ) << 14;
       $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
       $b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >> 2;

       imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
     }
   }
  }
}

?>

Code: Select all

//Calculate new Height and Width
$width = $height = 100;
list($width_orig, $height_orig) = getimagesize($filename);
if ($width_orig <= $width && $height_orig <= $height) {
    $width = $width_orig;
    $height = $height_orig;
} elseif ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
} else {
    $height = ($width / $width_orig) * $height_orig;
}

$image_p = imagecreatetruecolor($width, $height);
switch ($filetype) {
case '.jpg':
    $image = imagecreatefromjpeg($filename);
	break;
case '.gif':
    $image = imagecreatefromgif($filename);
	break;
default:
    break;
}

imagecopyresamplebicubic($image_p,$image,0,0,0,0,$width,$height,$width_orig,
  $height_orig);
imagejpeg($image_p);
There really should be a tutorial for this somewhere, but these code snippets cover the salient points... if you can understand them. Muahahaha.

Posted: Sat Nov 19, 2005 12:20 am
by waradmin
I think i understand the general points, making them into a form that puts the values in a mysql db is another story. I wouldnt know where to begin.

Posted: Sat Nov 19, 2005 12:29 am
by josh
Do you know how to handle a file upload? Do you know any mysql at all? You probably want to store the uploaded images and use mysql as an index. General file uploading is explained on http://www.php.net

Posted: Sat Nov 19, 2005 4:12 pm
by waradmin
I know how to do file uploads, but storing the dimensions as well as path in a db is what i dont know how to do.