imagescreatetruecolor with a white background

后端 未结 3 1601
半阙折子戏
半阙折子戏 2020-12-31 03:23

I\'m pretty sure I need to use imagefilledrectangle in order to get a white background instead of black on this... just not sure how. I\'ve tried a few ways.



        
相关标签:
3条回答
  • 2020-12-31 03:49

    imagefill() uses flood fill, which is quite slow compared to just painting a color in a rectangle without regard for the content of the image. So imagefilledrectangle() will be a lot quicker.

    // get size of target image
    $width  = imagesx($targetImage);
    $height = imagesy($targetImage);
    
    // get the color white
    $white  = imagecolorallocate($targetImage,255,255,255);
    
    // fill entire image (quickly)
    imagefilledrectangle($targetImage,0,0,$width-1,$height-1,$white);
    

    Speed is often a consideration when writing code.

    0 讨论(0)
  • 2020-12-31 03:58

    UPDATE (2020): Please see this answer below for a faster fill than imagefill: https://stackoverflow.com/a/32580839/1005039

    ORIGINAL

    From the PHP manual entry for imagefill:

    $image = imagecreatetruecolor(100, 100);
    
    // set background to white
    $white = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $white);
    
    0 讨论(0)
  • 2020-12-31 04:03
    $targetImage = imagecreatetruecolor($thumbw,$thumbh);
    
    // get the color white
    $color = imagecolorallocate($targetImage, 255, 255, 255);
    
    // fill entire image
    imagefill($targetImage, 0, 0, $color);
    

    imagecolorallocate: http://www.php.net/manual/en/function.imagecolorallocate.php

    imagefill: http://php.net/manual/en/function.imagefill.php

    0 讨论(0)
提交回复
热议问题