public function getFile($id, $size = false) { $file = $this->where(array('id' => (int) $id))->fetchOne(); if (empty($file)) { return false; } $dir = UPLOAD_DIR . DS . 'attachment' . DS; $temp = TEMP_DIR . DS . 'attachment'; File::mkdir($temp); if (!$size) { return $dir . $file['file']; } if ($size) { $tempfile = $temp . DS . $size . '-' . $file['file']; if (file_exists($tempfile)) { return $tempfile; } $resize = new Resize($dir . $file['file']); $sizes = getimagesize($dir . $file['file']); $index = 1; if ($sizes[0] >= $sizes[1]) { $index = $sizes[1] / $size; } else { $index = $sizes[0] / $size; } if ($index < 1) { $index = 1; } $resize->resizeImage(ceil($sizes[0] / $index), ceil($sizes[1] / $index)); $resize->saveImage($tempfile); return $tempfile; } }
function imageResize($file, $newWidth, $newHeight, $savePath, $option = null, $quality = null) { $r = new Resize($file); $r->resizeImage($newWidth, $newHeight, $option); $r->saveImage($savePath, $quality); }
public static function uploadImage($file_path = '', $file = '', $prefix = 'file', $uploadInfo = array(), $type = 'normal') { $path_orig = $file_path; if ($file['name'] != '') { $file_name_orig = $file['name']; $file_name = self::fileNameNew($file, $prefix); if (!is_dir($path_orig)) { mkdir($path_orig, 0777, true); } if (move_uploaded_file($file['tmp_name'], $path_orig . $file_name)) { switch ($type) { case 'square': foreach ($uploadInfo as $key => $value) { $directory = $file_path . $key; if (!is_dir($directory)) { mkdir($directory, 0777, true); } self::thumbSquare($path_orig . $file_name_orig, $directory . $file_name, 100, $value['width'], $value['height']); } break; case 'normal': foreach ($uploadInfo as $key => $value) { $directory = $file_path . $key; if (!is_dir($directory)) { mkdir($directory, 0777, true); } // echo $value['height']; // echo $value['width']; // exit; if ($value['width'] == 70 || $value['width'] == 270 || $value['width'] == 500) { $option = 'crop'; $Resizer = new Resize($path_orig . $file_name); $Resizer->resizeImage($value['width'], $value['height'], $option); $Resizer->saveImage($directory . $file_name, 100); } else { self::thumbNormal($path_orig . $file_name, $directory . $file_name, 100, $value['width'], $value['height']); } } break; case 'auto': foreach ($uploadInfo as $key => $value) { $directory = $file_path . $key; if (!is_dir($directory)) { mkdir($directory, 0777, true); } $option = 'auto'; $Resizer = new Resize($path_orig . $file_name); $Resizer->resizeImage($value['width'], $value['height'], $option); $Resizer->saveImage($directory . $file_name, 100); } break; case 'crop': foreach ($uploadInfo as $key => $value) { $directory = $file_path . $key; if (!is_dir($directory)) { mkdir($directory, 0777, true); } $option = 'crop'; $Resizer = new Resize($path_orig . $file_name); $Resizer->resizeImage($value['width'], $value['height'], $option); $Resizer->saveImage($directory . $file_name, 100); } break; default: } // die('file uploaded'); return $file_name; } else { die('file NOT uploaded'); return false; } } }
function getImageSize($id, $user_id, $size) { $photo = $this->getPhotoByID($id); $file = $photo; if (empty($file)) { return false; } $dir = ConnectionHelper::getPhotoDir($user_id) . 'original/'; $temp = TEMP_DIR . DS . 'avatars'; File::mkdir($temp); if (!$size) { return $dir . $file['file']; } if ($size) { $tempfile = $temp . DS . 'user-' . $user_id . '-' . $id . '-' . $size . '-' . $file['file']; if (file_exists($tempfile)) { return $tempfile; } $resize = new Resize($dir . $file['file']); $sizes = getimagesize($dir . $file['file']); $index = 1; if ($sizes[0] >= $sizes[1]) { $index = $sizes[1] / $size; } else { $index = $sizes[0] / $size; } if ($index < 1) { $index = 1; } $resize->resizeImage(ceil($sizes[0] / $index), ceil($sizes[1] / $index)); $resize->saveImage($tempfile); return $tempfile; } }
function addCategoryImg($id) { if (isset($_FILES['image']['name']) && $_FILES['image']['name']) { $name = $_FILES['image']['name']; $arr = explode('.', $name); $ext = end($arr); $rand = rand(1000000, 9999999); $filename = $rand . '.' . $ext; $path = APP . 'webroot/profile/' . $filename; move_uploaded_file($_FILES['image']['tmp_name'], $path); $resizeObj = new Resize($path); $resizeObj->resizeImage(250, 180, 'exact'); $resizeObj->saveImage(APP . 'webroot/profile_resize/' . $filename, 100); //echo $path;die(); unset($resizeObj); $resizeObj = new Resize($path); $resizeObj->resizeImage(600, 432, 'exact'); $resizeObj->saveImage('profile_resize1/' . $filename, 100); unlink($path); } $this->loadModel('Galleryimgs'); $arr['img'] = $filename; $arr['title'] = $_POST['category-img-title']; $arr['cat_id'] = $id; $this->Galleryimgs->create(); $this->Galleryimgs->save($arr); $this->redirect('/dashboard/gallery'); }