使用php GD投影角度

时间:2014-04-12 17:19:29

标签: php image-processing gd

我试图在搜索之后使用php GD创建一个图像的阴影我从来没有找到任何在我的图像上执行此操作的脚本所以我决定为自己编写一个这是我到目前为止尝试但是当我运行它时生成错误

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\xampp\htdocs\Image\dropshadow.php on line 29

这是更好理解的完整代码

$gdimg = imagecreatefromjpeg('3.jpg');
 shadowImage($gdimg,12,2,'000000',215,10);

 function shadowImage($gdimg,$distance,$width,$hexcolor,$angle,$fade)
  {

  $width_shadow = cos(deg2rad($angle)) * ($distance + $width);
  $height_shadow = sin(deg2rad($angle)) * ($distance + $width);


  $imgX = imagesx($gdimg);
  $imgY = imagesy($gdimg);

  $offset['x'] = cos(deg2rad($angle)) * ($distance + $width - 1);
  $offset['y'] = sin(deg2rad($angle)) * ($distance + $width - 1);

  $tempImageWidth = $imgX + abs($offset['x']);
  $tempImageHeight = $imgY + abs($offset['y']);
  $gdimg_dropshadow_temp = imagecreatetruecolor($tempImageWidth, $tempImageHeight);
  imagealphablending($gdimg_dropshadow_temp,false);
  imagesavealpha($gdimg_dropshadow_temp,true);
  $transparent1 = imagecolorallocatealpha($gdimg_dropshadow_temp,0,0,0,127);
  imagefill($gdimg_dropshadow_temp,0,0,$transparent1);
  for ($x = 0; $x < $imgX; $x++) {
    for ($y = 0; $y < $imgY; $y++) {

        $colorat = imagecolorat($gdimg, $x, $y);
        $PixelMap[$x][$y] = imagecolorsforindex($gdimg, $colorat);
      }
  }

  /* Creates the shadow */
  $r = hexdec(substr($hexcolor, 0, 2));
  $g = hexdec(substr($hexcolor, 2, 2));
  $b = hexdec(substr($hexcolor, 4, 2));

  /* Essentially masks the original image and creates the shadow */
  for ($x = 0; $x < $tempImageWidth; $x++) {
    for ($y = 0; $y < $tempImageHeight; $y++) {
        if (!isset($PixelMap[$x][$y]['alpha']) ||
             ($PixelMap[$x][$y]['alpha'] > 0)) {

                if (isset($PixelMap[$x + $offset['x']][$y + $offset['y']]['alpha']) && ($PixelMap[$x + $offset['x']][$y + $offset['y']]['alpha'] < 127)) {
                    $thisColor = imagecolorallocatealpha($gdimg_dropshadow_temp ,$r, $g, $b, $PixelMap[$x + $offset['x']][$y + $offset['y']]['alpha']);
                    imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
                    }
                }
             }
        }

  /* Overlays the original image */
  imagealphablending($gdimg_dropshadow_temp,true);

  for ($x = 0; $x < $imgX; $x++) {
    for ($y = 0; $y < $imgY; $y++) {
        if ($PixelMap[$x][$y]['alpha'] < 127) {
            imagecolorallocatealpha($gdimg_dropshadow_temp, $PixelMap[$x][$y]['red'], $PixelMap[$x][$y]['green'], $PixelMap[$x][$y]['blue'], $PixelMap[$x][$y]['alpha']);
            imagesetpixel($gdimg_dropshadow_temp, $x, $y, $thisColor);
        }
    }
  }

  imagesavealpha($gdimg, true);
  imagealphablending($gdimg, false);

  // Merge the shadow and the original into the original.
  $src_w = imagesx($gdimg_dropshadow_temp);
  $src_h = imagesy($gdimg_dropshadow_temp);
  imagecopyresampled($gdimg, $gdimg_dropshadow_temp, 0, 0, 0, 0, $imgX, $imgY,$src_w,$src_h);

  imagejpeg($gdimg, "shadowImage.jpg");

  }

请让我纠正。并告诉我如何创建图像的阴影

0 个答案:

没有答案