imageUID = $this->newImageUID(); } else { // type specific data loading global $kdb; $typeData = $kdb->getTypeData($rid, self::$table); if ( !$typeData ) { // no image uploaded? } else { $this->imageUID = $typeData['imageUID']; if ($this->imageUID==NULL) { $this->imageUID = $this->newImageUID(); } $this->imageUID = $typeData['imageUID']; $this->imageName = $typeData['imageName']; $this->size = $typeData['size']; $this->mime = $typeData['mime']; $this->width = $typeData['width']; $this->height = $typeData['height']; } } } /** * @Secured('roles' = {'authenticated'}, 'valid' = {}) * */ public function actionSave() { $this->updateData($_POST); $this->save(); return array('rid' => $this->getResourceID(), 'mode' => 'view'); } static function getThumbnail($rid) { return "images.php?rid=".$rid."&thumb=1"; } public function getImageURL() { return "images.php?rid=".$this->getResourceID().""; } public function getThumbnailURL() { return "images.php?rid=".$this->getResourceID()."&thumb=1"; } public function newImageUID() { $ts = date('YmdHis'); $rn = rand(1000,9999); return "image".$ts.$rn; } public function getImageUID() { return $this->imageUID; } public function getImageName() { return $this->imageName; } public function getImageSize() { return $this->size; } public function getImageMime() { return $this->mime; } public function getImageWidth() { return $this->width; } public function getImageHeight() { return $this->height; } public function makeXML() { $xml = 'getImageUID().']]>\n'; $xml .= 'getImageName().']]>\n'; $xml .= 'getImageSize().']]>\n'; $xml .= 'getImageMime().']]>\n'; $xml .= 'getImageWidth().']]>\n'; $xml .= 'getImageHeight().']]>\n'; return $xml; } public function makeImageFromXML($xml, $imgfile) { parent::makeResourceFromXML($xml); if ($this->getResourceID()!="new"){ if ( $imgfile ) { $imname = $xml['imageName']; $fsize = $xml['size']; $wid = $xml['width']; $hei = $xml['height']; $mime = $xml['mime']; $iuid = $xml['imageUID']; $q = "INSERT INTO images (resourceID, imageUID, imageName, size, mime, width, height) values (".$this->getResourceID().", '".$iuid."', '".$imname."', ".$fsize.", '".$mime."', ".$wid.", ".$hei.")"; $this->kdb->query($q); // resize $maxwidth=180; $maxheight=80; $ulfile = IMAGES_PATH.$this->getResourceID(); $imm = imagecreatefromstring($imgfile); $u_im = imageCreateTrueColor($wid,$hei); imageCopyResampled($u_im, $imm, 0, 0, 0, 0, $wid, $hei, $wid, $hei); imagejpeg($u_im, $ulfile); $currwidth=imagesx($imm); $currheight=imagesy($imm); if ( $currheight > $maxheight ) { $zoom = $maxheight/$currheight; $newwidth = $currwidth*$zoom; $newheight = $currheight*$zoom; } if ( $newwidth > $maxwidth ) { $zoom = $maxwidth/$newwidth; $newwidth = $currwidth*$zoom; $newheight = $currheight*$zoom; } //print "curr:".$currwidth." ".$currheight."
"; //print "new:".$newwidth." ".$newheight."
"; $t_im = imageCreateTrueColor($newwidth,$newheight); imageCopyResampled($t_im, $imm, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); imagejpeg($t_im, $ulfile."_thumb"); } else { // invalid image return 1; } } } /* * Handles image upload * 1. extracts and inserts in to images table * 2. moves to right place * 3. creates a thumbnail * */ protected function save() { $oldrid = $this->getResourceID(); parent::save(); $newrid = $this->getResourceID(); if ( isset($_FILES['imfile']) && is_uploaded_file($_FILES['imfile']['tmp_name']) ) { $imsize = getimagesize($_FILES['imfile']['tmp_name']); if ( $imsize != false ) { global $kdb; $imname = basename($_FILES['imfile']['name']); $fsize = $_FILES['imfile']['size']; $wid = $imsize[0]; $hei = $imsize[1]; $mime = $imsize['mime']; if ( $oldrid == $newrid ) { // existing object $dql = "DELETE FROM images WHERE resourceID=".$newrid; $kdb->query($dql); } $q = "INSERT INTO images (resourceID, imageUID, imageName, size, mime, width, height) values (".$newrid.", '".$this->getImageUID()."', '".$imname."', ".$fsize.", '".$mime."', ".$wid.", ".$hei.")"; $kdb->query($q); $ulfile = IMAGES_PATH.$newrid; if ( !move_uploaded_file($_FILES['imfile']['tmp_name'], $ulfile) ) { // moving problems return 3; } // resize $maxwidth=180; $maxheight=80; $fb = fopen($ulfile, "rb"); $imgdata = ""; while ( !feof($fb) ) { $imgdata .= fread($fb, 8124); } fclose($fb); $imm = imagecreatefromstring($imgdata); $currwidth=imagesx($imm); $currheight=imagesy($imm); if ( $currheight > $maxheight ) { $zoom = $maxheight/$currheight; $newwidth = $currwidth*$zoom; $newheight = $currheight*$zoom; } if ( $newwidth > $maxwidth ) { $zoom = $maxwidth/$newwidth; $newwidth = $currwidth*$zoom; $newheight = $currheight*$zoom; } //print "curr:".$currwidth." ".$currheight."
"; //print "new:".$newwidth." ".$newheight."
"; $t_im = imageCreateTrueColor($newwidth,$newheight); imageCopyResampled($t_im, $imm, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); imagejpeg($t_im, $ulfile."_thumb"); } else { // invalid image return 1; } } else { // no image return 2; } } } ?>