我在控制器中调度了php gd,这是代码
class getSmallImageController extends Controller
{
public function getImg(request $request)
{
// Get the image from local file. The image must be 300*200 size.
//change $img to your own path
$img_path = '/home/jonnyy/PhpstormProjects/zcapt/app/repository/Image/1.jpg';
$img = imagecreatefromjpeg($img_path);
$img_size = getimagesize($img_path);
if ($img_size[0] != 300 || $img_size[1] != 200)
die("image size must be 300*200");
//get value of authID from init.php
$response = app('App\Http\Controllers\initController')->index($request);
$authID = $response->getOriginalContent()['authID'];
// Calculate the diagonal coordinate for that square
$position_x_1 = DB::table('inits')
->select('x')
->where('authID', '=', $authID)
->inRandomOrder()
->first();
$position_y_1 = DB::table('inits')
->select('y')
->where('authID', '=', $authID)
->inRandomOrder()
->first();
// Create a small image with height 50 and width 50
$img_small = imagecreatetruecolor(50, 50);
// Copy one part of the large image (the image with size 300*200) to small part of image
imagecopy($img_small, $img, 0, 0, current($position_x_1), current($position_y_1), 50, 50);
// Change brightness of the picture
imagefilter($img_small, IMG_FILTER_BRIGHTNESS, 50);
$position_1 = 0;
$position_2 = 50;
// Adding some blur in to small picture
for ($i = 50; $i < 120; $i = $i + 6) {
imagerectangle($img_small, $position_1, $position_1, $position_2, $position_2, imagecolorallocatealpha($img_small, 255, 255, 255, $i));
$position_1 = $position_1 + 1;
$position_2 = $position_2 - 1;
}
// Set header in type jpg
;
// Generate image
header("Content-type: image/jpg");
// Generate image
imagejpeg($img_small);
// Release memory
imagedestroy($img_small);
;
imagedestroy($img);
}
}
但是当我dd()代码的最后一行时,
dd(imagedestroy($img))
顺便说一下,GD库在我调度的另一个控制器中工作。
答案 0 :(得分:1)
那不是带有MVC的Laravel应该工作的方式。作为肮脏的解决方案,我认为可以将exit;
放在imagedestroy()
之后。另一种选择是将图像保存到磁盘并显示给用户。
以下选项是保存在内存中,然后发送给用户。如果您的页面/应用程序访问得不太频繁,那么它会很适合您。
ob_start();
imagejpeg($img_small);
imagedestroy($img_small);
$image_data = ob_get_contents();
ob_end_clean();
$response = \Response::make($image_data, 200);
$response->header("Content-Type", "image/jpeg");
return $response;