note 28228 deleted from function.imagecopyresampled by cece
| From: | cece@php.net | Date: | Sat, 16 Aug 2003 19:59:02 +0000 |
| Subject: | note 28228 deleted from function.imagecopyresampled by cece | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-54442@lists.php.net to get a copy of this message | ||
Note Submitter: datasage@theworldisgrey.com
----
Here is my function to thumbnail and post the thumb and image to a database. I have the thumbnail
generation make the largest dimension 100px and scale the other dimension accordingly.
function PhotoToDB($picvarname, $linkto, $addedby, $caption, $id = ''){
$dblink=connectdb();
$date = Date("D M j G:i T Y");
//This section will add a new photo
if(is_uploaded_file($_FILES[$picvarname]['tmp_name'])){
$ext=substr($_FILES[$picvarname]['name'], -4);
if (strcasecmp($ext,".jpg") || strcasecmp($ext,".gif")){
if($ext==".jpg"){
$src_img = imagecreatefromjpeg($_FILES[$picvarname]['tmp_name']);
}elseif($ext==".gif"){
$src_img = imagecreatefromgif($_FILES[$picvarname]['tmp_name']);
}
$imageinfo = getimagesize($_FILES[$picvarname]['tmp_name']);
if($imageinfo[0]==$imageinfo[1]){
$new_w = 100;
$new_h = 100;
}elseif($imageinfo[0]>$imageinfo[1]){
$new_w = 100;
$ratio = $imageinfo[0]/100;
$new_h = $imageinfo[1]/$ratio;
}elseif($imageinfo[0]<$imageinfo[1]){
$new_h = 100;
$ratio = $imageinfo[1]/100;
$new_w = $imageinfo[0]/$ratio;
}
$dst_img = imagecreatetruecolor($new_w,$new_h);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img),
imagesy($src_img));
srand((double)microtime()*1234567);
$randint=rand(0,1000);
$pathstring="/tmp/thumb_" . $randint . ".jpg";
imagejpeg($dst_img, $pathstring, 75);
$contentsthumb = addslashes(fread(fopen($pathstring, "r"), filesize($pathstring)));
$contents = addslashes(fread(fopen($_FILES[$picvarname]['tmp_name'], "r"),
$_FILES[$picvarname]['size']));
$query="INSERT INTO jf_pictures (Caption, Picture, Thumb, DateAdd, AddBy, LinkTo, ImgType)
Values('" . $caption . "','" . $contents . "','" .
$contentsthumb . "','" . $date . "','" . $addedby .
"','" . $linkto . "','" .
$_FILES[$picvarname]['type'] . "')";
query($query, $dblink);
return'Photo added';
}
}elseif($_FILES[$picvarname]['error']=='UPLOAD_ERR_INI_SIZE' ||
'UPLOAD_ERR_FORM_SIZE'){
return 'File To Big';
}elseif($_FILES[$picvarname]['error']=='UPLOAD_ERR_PARTIAL'){
return 'File Not Uploaded';
}elseif($_FILES[$picvarname]['error']=='UPLOAD_ERR_NO_FILE'){
//Just update caption
$query="UPDATE jf_pictures SET Catption='" . $caption . "' WHERE
ID=" . $id;
query($query, $dblink);
return 'Caption updated';
}
}
To retrive the pictures. I just use a simple query script.