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.
Upload, resize, and add to db an image
Moderator: General Moderators
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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);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