【问题标题】:Use PHP GD in Laravel get garbled在 Laravel 中使用 PHP GD 会出现乱码
【发布时间】:2018-11-23 17:23:13
【问题描述】:

我在我的控制器中调度 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 库在我派出的另一个控制器中工作。

【问题讨论】:

    标签: php laravel php-gd


    【解决方案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;
    

    【讨论】:

    • 谢谢你的建议,它对我有用,无论如何我将来会重建我的代码,因为我只是新的 laravel 工作。lol。
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多