public function resizeImage($source, $width, $height, $type = 'ratio') { $assetPath = $this->imgResizePath; $fullPathSource = $source; if (!file_exists($fullPathSource) || !is_file($fullPathSource)) { return false; } $ext = strtolower(substr(strrchr($source, '.'), 1)); $fileName = strtolower(substr(strrchr($source, '/'), 1)); $folderType = substr($type, 0, 1); $source = str_replace($this->imgOrgPath, "", $source); //$rzname = strtolower(substr($source, 0, strpos($source,'.')))."_{$width}_{$height}.{$ext}"; $rzname = strtolower(substr($source, 0, strrpos($source, '/'))) . "/{$width}_{$height}_{$folderType}/{$fileName}"; $rzname = strtolower(substr($rzname, 0, strpos($rzname, '.'))) . "/" . md5_file($fullPathSource) . ".jpg"; if (file_exists($assetPath . DS . $rzname)) { return $rzname; } //empty dir first Utils::emptyDir(dirname($assetPath . DS . $rzname)); Utils::makeDir($assetPath); if (!file_exists($assetPath) && !mkdir($assetPath, 0755)) { return $source; } //$folders = explode('/',$source); $folders = explode('/', $rzname); $tmppath = $assetPath . DS; for ($i = 0; $i < count($folders) - 1; $i++) { if (!file_exists($tmppath . $folders[$i]) && !mkdir($tmppath . $folders[$i], 0755)) { return $source; } $tmppath = $tmppath . $folders[$i] . '/'; } if (!file_exists($assetPath . "/tmp")) { mkdir($assetPath . "/tmp", 0755); } list($r_width, $r_height) = getimagesize($fullPathSource); $imgCrop = new ImageCrop($fullPathSource, 0, 0, $r_width, $r_height, $assetPath . "/tmp"); switch ($type) { case "fix": $imgCrop->resizeFix($assetPath . DS . $rzname, $width, $height, 90); break; case "ratio": $imgCrop->resizeRatio($assetPath . DS . $rzname, $width, $height, 90); break; case "crop": default: $imgCrop->resizeCrop($assetPath . DS . $rzname, $width, $height, 90); break; } @chmod($assetPath . DS . $rzname, 0775); return $rzname; }
function createImage($model, $image_path) { if (file_exists($image_path)) { $fileSystem = new Filesystem(); $s0Path = $model->getAvatarPath($model->id, "s0", false); // Origin $s1Path = $model->getAvatarPath($model->id, "s1", false); // Resize $s2Path = $model->getAvatarPath($model->id, "s2", false); // Thumb $fileSystem->mkdirs(dirname($s0Path)); $fileSystem->mkdirs(dirname($s1Path)); $fileSystem->mkdirs(dirname($s2Path)); list($width, $height) = getimagesize($image_path); $imgCrop = new ImageCrop($image_path, 0, 0, $width, $height); $fileSystem->copy($image_path, $s0Path); $imgCrop->resizeFix($s1Path, 690, 250); $imgCrop->resizeFix($s2Path, 100, 100); $fileSystem->remove($image_path); } }