将十六进制颜色值存储在变量中,并在PHP中将其转换为JPEG

时间:2015-07-01 06:52:00

标签: php

我已经在merge.php中传递了hexacolor值并将其存储到变量中 现在。我想使用PHP将hexacolor值转换为jpeg。 所以我该怎么做。我的代码在哪里。

$bgcolor = $_POST['clickcolor']; // clickcolor store hexacolor like #4c1130
echo $bgcolor;
$im = imagecreate(640, 480)or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, $bgcolor);
header("Content-Type: image/jpeg");
imagejpeg($im);

但它不起作用。

2 个答案:

答案 0 :(得分:1)

f1 = open('scraping.log', 'a') page = br.open(url) html = page.read() soup = BeautifulSoup(html) for a in soup.select('a[href^="download.php?action=download"]'): link = a.attrs.get('href') print >>f1, link br.retrieve(url+link, destination) 需要四个参数,第一个是资源,然后是0到255之间的整数,或者是0x00和0xFF之间的十六进制。

<强>描述

imagecolorallocate

<强>示例:

int imagecolorallocate ( resource $image , int $red , int $green , int $blue )

Check the documentation

答案 1 :(得分:0)

您可以使用此代码:
使用hexColorAllocate函数从十六进制到所需。

header('Content-type: image/png');
      $png_image = imagecreate(150, 150);
      hexColorAllocate($png_image, '4c1130');
      imagepng($png_image);
      imagedestroy($png_image);


      function hexColorAllocate($im,$hex){
        $hex = ltrim($hex,'#');
        $a = hexdec(substr($hex,0,2));
        $b = hexdec(substr($hex,2,2));
        $c = hexdec(substr($hex,4,2));
        return imagecolorallocate($im, $a, $b, $c); 
    }
相关问题