Created
June 5, 2016 15:11
-
-
Save nickshek/8b068619c9ba9bda2ce941a70a0c11e2 to your computer and use it in GitHub Desktop.
Simple PlaceHolder API using Laravel and Intervention
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use Intervention\Image\ImageManager; | |
class PlaceHolderController extends Controller | |
{ | |
public function serve($width,$height,$background_color = "#000",$text=NULL){ | |
if($width <= 0){ | |
return redirect()->action("ImageController@not_found"); | |
} | |
if($height <= 0){ | |
return redirect()->action("ImageController@not_found"); | |
} | |
$manager = new ImageManager(); | |
$img = $manager->canvas($width, $height, $background_color); | |
if($text === NULL){ | |
$text = sprintf("%sx%s",$width,$height); | |
} | |
$font_size = 32; | |
if($width <= 150 && $height <= 100){ | |
$font_size = 16; | |
} | |
if($width >= 60 && $height >= 30){ | |
$img->text($text, $width/2, $height/2, function($font) use($font_size) { | |
$font->size($font_size); | |
$font->color(array(255, 255, 255)); | |
$font->file(base_path('resources/assets/fonts/wt014.ttf')); | |
$font->align('center'); | |
$font->valign('middle'); | |
}); | |
} | |
return $img->response('jpg'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment