I'm trying to use PHP shape drawing functions with the Laravel framework but I am having some trouble.
So, I just to test it out, basically just copied the example from php.net and used it in a closure and a test route.
Route::get('maptest', function()
{
$image = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($image, 227, 32, 32);
imagefilledrectangle($image, 100, 80, 50, 25, $red);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
});
doing it this way works fine.
the problem is when I throw a Controller and a View into the mix. When I set the headers and what not and create a Response, I get a little broken picture icon in the top left corner of the window and nothing on the page loads at all.
this is how I am setting the headers.
(I know this is posted everywhere, but none of the solutions I could find seemed to work for me.)
public function show($id)
{
$user = User::find($id);
$view = View::make('profile/home', ['user' => $user]);
$response = Response::make($view, 200);
$response->header('Content-type', 'image/png');
return $response;
}
So the result of the above code (inside my controller) gives me a little broken picture icon in top left corner of window.
Related
I'm trying to remove the white background in logos to get a transparent icon, for example
on this image
Is .jpg, I want to remove withe background (Only what's outside if is possible, otherwise removing all the white background)
I'm trying using laravel intervention image with this code
public function resizeImage($logo, $placeName, $domain)
{
$mask = Image::make($logo)
->contrast(100)
->contrast(50)
->trim('top-left', null, 40)
->invert(); // invert it to use as a mask
$new_image = Image::canvas($mask->width(), $mask->height(), '#000000')
->mask($mask)
->resize(32,32, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->encode('png', 100);
$route = $domain . '/favicon/favicon.png';
Storage::disk('gcs')
->put($route, $new_image);
$route = Storage::disk('gcs')
->url($route);
return $route;
}
but this is my result
the image lost color (black colored)
what am I doing wrong? I feel that I am very close to achieving it, however, I need the icons to have color
$img = Image::canvas(800, 600);
Call canvas method without background color.
See more on http://image.intervention.io/api/canvas
I use Laravel 5.1 queues to process large images. I've a very strange behavior since when I call a function "imagettfbbox" in the constructor it works. But obviously I need to make it work in the "handle" but there I got an error.
public function __construct()
{
//TEST
$font_path = public_path('/fonts/roboto/Roboto-Thin.ttf');
imagecreate(10,10); //works!
imagettfbbox(10, 0, $font_path, 'test'); //works!
}
public function handle() //GenerateImage $generator, Image $img
{
//TEST
print 'OK'; //gets printed
$font_path = public_path('/fonts/roboto/Roboto-Thin.ttf');
imagecreate(10,10); //works!
imagettfbbox(10, 0, $font_path, 'test'); //CRASHES!
}
I get the 'OK' printed and then the error "Call to undefined function App\Jobs\imagettfbbox()".
It is a very strange behavior since some image functions work other not. I've GD installed and everywhere outside handle the code works. Any clue what I'm missing here?
Maybe your are missing FreeType library that is needed by imagettfbbox
I want to have a Route that takes image Dimension and source and return Cropped and resized image via timthumb php script.
I put timthumb.php file in a folder in public directory and I wrote this Route:
Route::get('/showImage/{w}/{h}/{src}', function ($w , $h , $src) {
$img = 'public/plugins/timthumb/timthumb.php?src='.$src.'&w='.$w.'&h='.$h;
return Response::make($img, 200, array('Content-Type' => 'image/jpeg'));
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
But nothing happen.
How can I access to timthumb.php file and send it required parameters and get result image?
Update:
this is structure of Public directory and placement of timthumb and images folder :
According to , I try :
$img = 'public/plugins/timthumb/timthumb.php?src=../../'.$src.'&w='.$w.'&h='.$h;
And :
$img = 'public/plugins/timthumb/timthumb.php?src=public/'.$src.'&w='.$w.'&h='.$h;
But none of them does not work for bellow URL:
http://localhost:8000/showImage/100/200/upload/slideshow/slide2.jpg
I suggest using league/glide. Watch Using Glide in Laravel to get started.
The better way to use timthumb image in Laravel using intervention/image
Install this package.
Update your Route:
Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src)
{
$img_path = public_path().'/'.$src;
$img = Image::make($img_path)->resize($w, $h);
return $img->response('jpg');
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
OR Image Cache
Install intervention/imagecache package
Route::get('showImage/{w}/{h}/{src}', function ($w , $h , $src)
{
$img_path = public_path().'/'.$src;
$img = Image::cache(function($image)use($w,$h,$img_path) {
return $image->make($img_path)->resize($w, $h);
});
return Response::make($img, 200, ['Content-Type' => 'image/jpeg']);
})->where('src', '[A-Za-z0-9\/\.\-\_]+');
Image URL would be as below:
http://<< Domain Name >>/showImage/800/400/Desert.jpg
I'm having an issue with imagick running on a MAMP installation within a cakephp app.
I have followed the install instructions as indicated here and the install seems to have worked in the sense that the class 'Imagick' exists for me if i test it in a php script (I can see the module is loaded in phpinfo). However any examples that I run utilising the class hang as soon as I echo any content. My view is:
<?php
/* Create a new imagick object */
$im = new Imagick();
/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");
/* Create imagickdraw object */
$draw = new ImagickDraw();
/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);
/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);
/* Close the pattern */
$draw->popPattern();
/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');
/* Set font size to 52 */
$draw->setFontSize(52);
/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");
/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");
/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);
/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);
/* Set the format to PNG */
$canvas->setImageFormat('png');
/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>
This script will just hang as soon as the echo $canvas is encountered. The script works perfectly in a plain old php file i.e outside of cake but it hangs when visited via my cakephp app action. My action code is:
public function test(){
$this->layout = false;
}
Cake error log is empty.
Okay the issue was with how the header was set in cakephp. Cake didn't allow me to set the page header in the view that way.
I added the following code to the test action:
$this->response->type("image/png");
And it works perfectly now.
my action:
public function executePreview(sfWebRequest $request)
{
$this->setLayout('layout_preview');
$text='';
$text= $request->getGetParameter('text');
$this->img='';
$this->img2=$this->createImage(array('font_size'=>10, 'line_spacing'=>1.5,'font'=>'dejavu', 'angle'=>10,
'preset'=>'BRCA', 'text'=>$text));
}
public function createImage( $params)
{
$x=0;
$y=0;
$interval= $params['font_size']*$params['line_spacing'];
$src_image= imagecreatefromjpeg(sfConfig::get('sf_web_dir').'/images/'.$params['preset'].'.jpg');
$black = imagecolorallocate($src_image, 0, 0, 0);
$lines=explode("\n", $params['text']);
putenv('GDFONTPATH='.join(':',sfConfig::get('app_font_path')));
$fontname=$params['font'].'.ttf';
foreach($lines as $i=>$line):
imagettftext($src_image, $params['font_size'], $params['angle'], $x, $y+$i*$interval, $black, $fontname, $line);
endforeach;
return $src_image;
}
in my template:
<?php
imagejpeg($img2);
?>
but when trying to GET /modulename/preview?preview=something&text=somethingelse, it gives an error. viewing the source of the webpage obtained, I see :
<title>InvalidArgumentException: Unable to escape value "NULL".</title>
Can I not pass resource identifier to the template? What can be a way around that? I need this createImage function elsewhere also, I'm just trying to follow DRY
imagejpeg creates the image and returns a boolean, i don't get why you call it in your view, it should stay to your action, actually i wouldn't use view at all in your case.
try something like :
$this->getResponse()->setHttpHeader('Content-type', 'image/jpeg');
$this->getResponse()->setContent($src_image);
$this->setLayout(false);
or directly setting header with PHP and returning sfView::HEADER_ONLY