Resizes image according to the given width (height proportional)
public resizeToWidth ( integer $width, boolean $allow_enlarge = false ) : static | ||
$width | integer | |
$allow_enlarge | boolean | |
return | static |
public function generateThumbnail($name) { $image = new ImageResize(self::PATH_ORIGINALS . DIRECTORY_SEPARATOR . $name); $image->resizeToWidth(1900); unlink(self::PATH_ORIGINALS . DIRECTORY_SEPARATOR . $name); $image->save(self::PATH_ORIGINALS . DIRECTORY_SEPARATOR . $name); $image = new ImageResize(self::PATH_ORIGINALS . DIRECTORY_SEPARATOR . $name); $image->resizeToWidth(200); $image->save(self::PATH_THUMBNAILS . DIRECTORY_SEPARATOR . $name); }
/** * Resize an image and optionally rename it. * * @param string $path * @param string $filename * @param int $width * @param string|null $newFilename */ public function resizeToWidth($path, $filename, $width, $newFilename = null) { $fullFilename = $path . '/' . $filename; $image = new ImageResize($fullFilename); $image->resizeToWidth($width, true); if (is_string($newFilename) && !empty($newFilename)) { $image->save($path . '/' . $newFilename); return; } $image->save($fullFilename); }
public function postImage(Request $request) { $validator = Validator::make($request->all(), ['height' => 'integer', 'width' => 'integer', 'image_file' => 'required|image|mimes:png,jpg,jpeg,gif,bmp']); if ($validator->fails()) { return redirect('/')->withErrors($validator)->withInput(); } // if pass validation $image_name = $request->file('image_file')->getClientOriginalName(); $image_extension = $request->file('image_file')->getClientOriginalExtension(); $image_new_name = md5(microtime(true)); $temp_file = base_path() . '/public/images/upload/' . strtolower($image_new_name . '_temp.' . $image_extension); $request->file('image_file')->move(base_path() . '/public/images/upload/', strtolower($image_new_name . '_temp.' . $image_extension)); $origin_size = getimagesize($temp_file); $origin_width = $origin_size[0]; $origin_height = $origin_size[1]; // resize $image_resize = new ImageResize($temp_file); if (trim($request->get('height')) != "") { $height = $request->get('height'); } else { $height = 0; } if (trim($request->get('width')) != "") { $width = $request->get('width'); } else { $width = 0; } if ($width > 0 && $height > 0) { $image_resize->resize($width, $height); } else { if ($width == 0 && $height > 0) { $image_resize->resizeToHeight($height); } else { if ($width > 0 && $height == 0) { $image_resize->resizeToWidth($width); } } } $image_resize->save(base_path() . '/public/images/upload/' . $image_new_name . '.' . $image_extension); $image_location = '/images/upload/' . $image_new_name . '.' . $image_extension; File::delete($temp_file); $image_data = array('image_name' => $image_name, 'image_extension' => $image_extension, 'image_location' => $image_location, 'origin_height' => $origin_height, 'origin_width' => $origin_width, 'height' => $height, 'width' => $width); $this->image_gestion->saveImage($image_data); return redirect('/')->with('message', 'Successfully upload image!'); }
public function uploadFile() { $this->filename = $this->file->baseName . '.' . $this->file->extension; $this->avatar = Yii::$app->security->generateRandomString() . '.' . $this->file->extension; $this->type = $this->file->type; if ($this->validate()) { $this->save(false); $filePath = Yii::$app->params['imagePath'] . $this->avatar; $image = new ImageResize($this->file->tempName); if ($image->getSourceWidth() > 1200) { $image->resizeToWidth(1200)->save($filePath); } else { $this->file->saveAs($filePath); } return true; } return false; }
/** * @param string $repo * @param string $file * @param int $width * @param bool $archived * @param \Slim\Http\Response $response * @return mixed */ function thumb($repo, $file, $width, $archived, $response, $format) { $md5 = md5($file); $file = $md5[0] . '/' . $md5[0] . $md5[1] . '/' . $file; $path = $repo . ($archived ? 'archive/' : '') . $file; if (is_readable($path)) { $path = realpath($path); $pathParts = pathinfo($path); if (strpos($pathParts['dirname'], $repo) === 0) { $cacheDir = $repo . 'thumb/' . ($archived ? 'archive/' : '') . $file; if (!is_dir($cacheDir)) { if (!mkdir($cacheDir, 0777, true)) { return $response->withStatus(403); } } if ($format != 'jpg') { $cacheFile = $cacheDir . '/' . $width . 'px-' . $pathParts['basename']; } else { $cacheFile = $cacheDir . '/' . $width . 'px-' . $pathParts['filename'] . '.jpg'; } if (!is_readable($cacheFile)) { $image = new ImageResize($path); if ($width > $image->getSourceWidth()) { return $response->withRedirect('/images/' . $file); } else { $image->resizeToWidth($width); if ($format != 'jpg') { $image->save($cacheFile); } else { $image->save($cacheFile, IMAGETYPE_JPEG); } } } $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $cacheFile); $stream = new \GuzzleHttp\Psr7\LazyOpenStream($cacheFile, 'r'); return $response->withHeader('Content-type', $type)->withBody($stream); } } return $response->withStatus(404); }
public function testResizeToWidth() { $image = $this->createImage(200, 100, 'png'); $resize = new ImageResize($image); $resize->resizeToWidth(100); $this->assertEquals(100, $resize->getDestWidth()); $this->assertEquals(50, $resize->getDestHeight()); }
<?php include "ImageResize.php"; use Eventviva\ImageResize; $url = $_POST['img']; $image = new ImageResize($url); $image->resizeToWidth(500, 500); $path_dissect = pathinfo($url); $dis = strtok($path_dissect['basename'], '?'); $img = 'dp/' . $dis; $image->save($img); //file_put_contents($img, file_get_contents($url)); //echo "http://cryptlife.com/prayforchennai/".$img; /*$png = imagecreatefromjpeg($img); $jpeg = imagecreatefrompng('overlay.png'); list($width, $height) = getimagesize('overlay.png'); list($newwidth, $newheight) = getimagesize($img); $out = imagecreatetruecolor("500", "500"); imagecopyresampled($out, $jpeg, 0, 0, 0, 0, "500", "500", $width, $height); imagecopyresampled($out, $png, 0, 0, 0, 0, "500", "500", $newwidth, $newheight); imagejpeg($out, 'final/'.$dis, 100);*/ /*$png = imagecreatefrompng('overlay.png'); $jpeg = imagecreatefromjpeg($img); list($width, $height) = getimagesize('overlay.png'); list($newwidth, $newheight) = getimagesize($img); $out = imagecreatetruecolor($newwidth, $newheight); imagecopyresampled($out, $jpeg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagecopyresampled($out, $png, 0, 0, 0, 0, $newwidth, $newheight, $newwidth, $newheight); imagejpeg($out, 'final/'.$dis, 100);*/ $width = 500;
/** * @param string $file * @param int $width * @param int $height * @return void */ public function resizeAndSave($file, $width, $height) { $image = new ImageResize($_SERVER['DOCUMENT_ROOT'] . $file); if ($width == 'auto') { $image->resizeToHeight($height); } elseif ($height == 'auto') { $image->resizeToWidth($width); } else { $image->crop($width, $height); } unlink($_SERVER['DOCUMENT_ROOT'] . $file); $image->save($_SERVER['DOCUMENT_ROOT'] . $file); }
<?php ini_set('memory_limit', '1024M'); set_time_limit(0); include 'libs/php-image-resize/src/ImageResize.php'; use Eventviva\ImageResize; $path = "img/ivymod"; $files = scandir($path); $cont = 0; foreach ($files as $value) { if (!is_dir($value)) { echo $value . " **** "; $image = new ImageResize($path . '/' . $value); $image->resizeToWidth(840); $image->save('img/ivymod-840x1110/' . $value); echo "<a href='http://localhost/" . basename(__DIR__) . "/" . $path . "/840x1110" . $value . "' target='_blank' >" . $value . "</a><br/>"; //echo "$path/840x1110/$value <br>"; $cont++; //exit; } } echo "<br>Total: <b>" . $cont . "</b>";