Combining PHP classes, not populating array -
i've got 3 different classes (class thumbnail in thumbnail.php, class upload in upload.php , class thumbnailupload in thumbnailupload.php) in same namespace (photoproject). currently, load image, create thumbnail , store both in selected directory, not store information requested database. if in code snippet of blog_insert.php, once gets calling $loader->collectfilenames assigned $names, nothing ever stored in $names[]. i've did var_dump, , $names empty. suggestions? below code snippets.
class upload { protected $collectednames = []; protected $typecheckingon = true; protected $nottrusted = ['bin', 'cgi', 'exe', 'js']; protected $newname; protected $destination; protected $max = 8388608; protected $messages = []; protected $permitted = [ 'image/gif', 'image/png', 'image/jpg' ]; ... public function collectfilenames() { return $this->collectednames; }
blog_insert.php
<?php use photoproject\thumbnailupload; require_once '../includes/connection.php'; $conn = dbconnect(); if (isset($_post['insert'])) { $ok = false; $stmt = $conn->stmt_init(); // if file has been uploaded, process if(isset($_post['upload_new']) && $_files['image']['error'] == 0) { $imageok = false; require_once '../photoproject/thumbnailupload.php'; $loader = new thumbnailupload('path store images'); $loader->setthumbdestination('path store thumbnail'); $loader->upload(); $names = $loader->collectfilenames(); // $names empty array if upload failed if ($names) { $sql = 'insert images (filename, caption) values (?, ?)';
class thumbnail
<?php namespace photoproject; class thumbnail { protected $original; protected $originalwidth; protected $originalheight; protected $basename; protected $thumbwidth; protected $thumbheight; protected $maxsize = 350; protected $canprocess = false; protected $imagetype; protected $destination; protected $suffix = '_thb'; protected $messages = []; public function __construct($image) { if (is_file($image) && is_readable($image)) { $details = getimagesize($image); } else { $details = null; $this->messages[] = "cannot open $image."; } // if getimagesize() returns array, looks image if (is_array($details)) { $this->original = $image; $this->originalwidth = $details[0]; $this->originalheight = $details[1]; $this->basename = pathinfo($image, pathinfo_filename); // check mime type $this->checktype($details['mime']); } else { $this->messages[] = "$image doesn't appear image."; } } public function setdestination($destination) { if (is_dir($destination) && is_writable($destination)) { // last character $last = substr($destination, -1); // add trailing slash if missing if ($last == '/' || $last == '\\') { $this->destination = $destination; } else { $this->destination = $destination . directory_separator; } } else { $this->messages[] = "cannot write $destination."; } } public function setmaxsize($size) { if (is_numeric($size) && $size > 0) { $this->maxsize = abs($size); } else { $this->messages[] = 'the value setmaxsize() must positive number.'; $this->canprocess = false; } } public function setsuffix($suffix) { if (preg_match('/^\w+$/', $suffix)) { if (strpos($suffix, '_') !== 0) { $this->suffix = '_' . $suffix; } else { $this->suffix = $suffix; } } else { $this->suffix = ''; } } public function create() { if ($this->canprocess && $this->originalwidth != 0) { $this->calculatesize($this->originalwidth, $this->originalheight); $this->createthumbnail(); } elseif ($this->originalwidth == 0) { $this->messages[] = 'cannot determine size of ' . $this->original; } } public function getmessages() { return $this->messages; } protected function checktype($mime) { $mimetypes = ['image/jpeg', 'image/png', 'image/gif']; if (in_array($mime, $mimetypes)) { $this->canprocess = true; // extract characters after 'image/' $this->imagetype = substr($mime, 6); } } protected function calculatesize($width, $height) { if ($width <= $this->maxsize && $height <= $this->maxsize) { $ratio = 1; } elseif ($width > $height) { $ratio = $this->maxsize/$width; } else { $ratio = $this->maxsize/$height; } $this->thumbwidth = round($width * $ratio); $this->thumbheight = round($height * $ratio); } protected function createimageresource() { if ($this->imagetype == 'jpeg') { return imagecreatefromjpeg($this->original); } elseif ($this->imagetype == 'png') { return imagecreatefrompng($this->original); } elseif ($this->imagetype == 'gif') { return imagecreatefromgif($this->original); } } protected function createthumbnail() { $resource = $this->createimageresource(); $thumb = imagecreatetruecolor($this->thumbwidth, $this->thumbheight); imagecopyresampled($thumb, $resource, 0, 0, 0, 0, $this->thumbwidth, $this->thumbheight, $this->originalwidth, $this->originalheight); $newname = $this->basename . $this->suffix; if ($this->imagetype == 'jpeg') { $newname .= '.jpg'; $success = imagejpeg($thumb, $this->destination . $newname, 100); } elseif ($this->imagetype == 'png') { $newname .= '.png'; $success = imagepng($thumb, $this->destination . $newname, 0); } elseif ($this->imagetype == 'gif') { $newname .= '.gif'; $success = imagegif($thumb, $this->destination . $newname); } if ($success) { $this->messages[] = "$newname created successfully."; } else { $this->messages[] = "couldn't create thumbnail " . basename($this->original); } imagedestroy($resource); imagedestroy($thumb); }
}
class thumbnailupload
<?php namespace photoproject; use photoproject\upload; require_once 'upload.php'; require_once 'thumbnail.php'; class thumbnailupload extends upload { protected $thumbdestination; protected $deleteoriginal; protected $suffix = '_thb'; public function __construct($path, $deleteoriginal = false) { parent::__construct($path); $this->thumbdestination = $path; $this->deleteoriginal = $deleteoriginal; } /* ** setter method thumbnail destination */ public function setthumbdestination($path) { if (!is_dir($path) || !is_writable($path)) { throw new \exception("$path must valid, writable directory."); } $this->thumbdestination = $path; } public function setthumbsuffix($suffix) { if (preg_match('/\w+/', $suffix)) { if (strpos($suffix, '_') !== 0) { $this->suffix = '_' . $suffix; } else { $this->suffix = $suffix; } } else { $this->suffix = ''; } } public function allowalltypes() { $this->typecheckingon = true; } protected function createthumbnail($image) { $thumb = new thumbnail($image); $thumb->setdestination($this->thumbdestination); $thumb->setsuffix($this->suffix); $thumb->create(); $messages = $thumb->getmessages(); $this->messages = array_merge($this->messages, $messages); } protected function movefile($file) { $filename = isset($this->newname) ? $this->newname : $file['name']; $success = move_uploaded_file($file['tmp_name'], $this->destination . $filename); if ($success) { // add message if original image not deleted if (!$this->deleteoriginal) { $result = $file['name'] . ' uploaded successfully'; if (!is_null($this->newname)) { $result .= ', , renamed ' . $this->newname; } $this->messages[] = $result; } // create thumbnail uploaded image $this->createthumbnail($this->destination . $filename); // delete uploaded image if required if ($this->deleteoriginal) { unlink($this->destination . $filename); } } else { $this->messages[] = 'could not upload ' . $file['name']; } } }
class upload
<?php namespace photoproject; class upload { protected $collectednames = []; protected $typecheckingon = true; protected $nottrusted = ['bin', 'cgi', 'exe', 'js']; protected $newname; protected $renameduplicates; protected $destination; protected $max = 8388608; protected $messages = []; protected $permitted = [ 'image/gif', 'image/png', 'image/jpg' ]; public function __construct($path) { if (!is_dir($path) || !is_writable($path)) { throw new \exception("$path must valid, writable directory."); } $this->destination = $path; } public function setmaxsize($num) { if (is_numeric($num) && $num > 0) { $this->max = (int) $num; } } public function allowalltypes() { $this->typecheckingon = false; if (!$suffix) { $this->suffix = ''; // empty string } } public function upload($renameduplicates = true) { $this->renameduplicates = $renameduplicates; $uploaded = current($_files); if (is_array($uploaded['name'])) { // deal multiple uploads foreach ($uploaded['name'] $key => $value) { $currentfile['name'] = $uploaded['name'][$key]; $currentfile['type'] = $uploaded['type'][$key]; $currentfile['tmp_name'] = $uploaded['tmp_name'][$key]; $currentfile['error'] = $uploaded['error'][$key]; $currentfile['size'] = $uploaded['size'][$key]; if ($this->checkfile($currentfile)) { $this->movefile($currentfile); } } } else { if ($this->checkfile($uploaded)) { $this->movefile($uploaded); } } } public function getmessages() { return $this->messages; } public function collectfilenames() { return $this->collectednames; } public function getmaxsize() { return number_format($this->max/1024, 1) . ' kb'; } protected function checkfile($file) { $accept = true; if ($file['error'] != 0) { $this->geterrormessage($file); // stop checking if no file submitted if ($file['error'] == 4) { return false; } else { $accept = false; } } if (!$this->checksize($file)) { $accept = false; } if ($this->typecheckingon) { if (!$this->checktype($file)) { $accept = false; } } if ($accept) { $this->checkname($file); } return $accept; return true; } protected function checkname($file) { $this->newname = null; $nospaces = str_replace(' ', '_', $file['name']); if ($nospaces != $file['name']) { $this->newname = $nospaces; } $extension = pathinfo($nospaces, pathinfo_extension); if (!$this->typecheckingon && !empty($this->suffix)) { if (in_array($extension, $this->nottrusted) || empty($extension)) { $this->newname = $nospaces . $this->suffix; } } if ($this->renameduplicates) { $name = isset($this->newname) ? $this->newname : $file['name']; $existing = scandir($this->destination); if (in_array($name, $existing)) { // rename file $basename = pathinfo($name, pathinfo_filename); $extension = pathinfo($name, pathinfo_extension); $i = 1; { $this->newname = $basename . '_' . $i++; if (!empty($extension)) { $this->newname .= ".$extension"; } } while (in_array($this->newname, $existing)); } } } protected function geterrormessage($file) { switch($file['error']) { case 1: case 2: $this->messages[] = $file['name'] . ' big: (max: ' . $this->getmaxsize() . ').'; break; case 3: $this->messages[] = $file['name'] . ' partially uploaded.'; break; case 4: $this->messages[] = 'no file submitted.'; break; default: $this->messages[] = 'sorry, there problem uploading ' . $file['name']; break; } } protected function checksize($file) { if ($file['error'] == 1 || $file['error'] == 2 ) { return false; } elseif ($file['size'] == 0) { $this->messages[] = $file['name'] . ' empty file.'; return false; } elseif ($file['size'] > $this->max) { $this->messages[] = $file['name'] . ' exceeds maximum size file (' . $this->getmaxsize() . ').'; return false; } else { return true; } } protected function checktype($file) { if (in_array($file['type'], $this->permitted)) { return true; } else { if (!empty($file['type'])) { $this->messages[] = $file['name'] . ' not permitted type of file.'; return false; } } } protected function movefile($file) { $filename = isset($this->newname) ? $this->newname : $file['name']; $success = move_uploaded_file($file['tmp_name'], $this->destination . $filename); if ($success) { // add amended filename array of uploaded files $this->filenames[] = $filename; $result = $file['name'] . ' uploaded successfully'; if (!is_null($this->newname)) { $result .= ', , renamed ' . $this->newname; } $this->messages[] = $result; } else { $this->messages[] = 'could not upload ' . $file['name']; } } }
Comments
Post a Comment