Skip to content

Instantly share code, notes, and snippets.

@mahammad
Last active August 10, 2020 10:51
Show Gist options
  • Save mahammad/f14494f91051ed797b7a5e63d2b8cd27 to your computer and use it in GitHub Desktop.
Save mahammad/f14494f91051ed797b7a5e63d2b8cd27 to your computer and use it in GitHub Desktop.
Laravel image resizer from url
Route::get('resizer/{width?}/{height?}/{cp?}/{photo}', function ($width, $height, $cp, $photo) {
    // Resmi bulduk.
    $img = Image::make(public_path('cdn/' . $photo));
    // Resmin boyutlarını aldık
    $w  = $img->width();
    $h = $img->height();
    // Boyutlandırma işlemleri için değişkenleri tanımladık.
    $resizeWidth = $resizeHeight = null;
    // Bütün alanı kapla
    if($cp == 1){
        $resizeWidth = ($width > $w) ? $width : null;
        $resizeHeight = ($height > $h) ? $height : null;
    }
    // Sığdır
    elseif($cp == 2){
        $resizeWidth = ($width > $w) ? $height : null;
        $resizeHeight = ($height > $h) ? $width : null;
    }
    // Arkaplansız sığdır
    elseif($cp == 3){
        $resizeWidth = $width < $height == 'width' ? $width : null;
        $resizeHeight = $width > $height == 'height' ? $height : null;
    }
    $img->resize($resizeWidth, $resizeHeight, function ($constraint) {
        $constraint->aspectRatio();
    });
    if($cp == 1 || $cp == 2)
        $img->resizeCanvas($width, $height, 'center', false, '#ffffff');
    return $img->response();
})->name('api.resizer');
            ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment