doufei1852 2014-04-11 16:43
浏览 82
已采纳

PHP在循环中使用imagecreatefrompng函数从单个图像创建多个图像

I am creating multiple partial images from a single image to use for a map overlay. The issue I am having is that the first image is created successfully, but the rest are black. I suspect that the imagecreatefrompng is the culprit and it is not able to run multiple times with out clearing it and so my images are black because the image is nil. Please help me find a suitable solution to my code below. Thanks in advance.

Everything in my code works, except the image creation. As I said before it creates the image successfully once. The output of my loop says everything that it should say.

for($x = 0; $x <= $loop; $x++)
{
    //Check for direcotory for X
    $x_directory = 'generated_images/' . $dbn . '/' . $zoom . '/' . $x;
    if (!file_exists($x_directory)) 
    {
        mkdir($x_directory, 0777, true);
    }//end if

    //Set Starting Y Copy Values
    $startY = 0;
    $endY = 1;

    for($y = 0; $y <= $loop; $y++)
    {
        //Do not need to check for Y images, we will replace any exsisting images
        //Set a time limit for each Y image
        set_time_limit(15);
        $time = time();

        $src_image = imagecreatefrompng('generated_images/pre'. $dbn .'.png') or die('Problem with source');
        $y_image = imagecreatetruecolor($image_w,$image_h) or die('Problem In Creating image');

        if(($x >= 2 && $x <= 5) && ($y >= 5 && $y <= 6)) {
            ?>
            <p>
            Start X: <?=$startX?> <br/>
            Start Y: <?=$startY?> <br/>
            End X: <?=$endX?> <br/>
            End Y: <?=$endY?> <br/>
            <? 

            //imagecopyresized($y_image, $src_image, 0, 0, $startX, $startY, $image_w, $image_h, 1024, 1024);

            //Set the blending mode for an image
            imagealphablending($y_image, false);
            imagesavealpha($y_image, true);

            // scan image pixels
            for ($pix_y = ($startY * $multiplier); $pix_y < ($endY * $multiplier) ; $pix_y++) {

                for ($pix_x = ($startX *$multiplier); $pix_x < ($endX * $multiplier) ; $pix_x++) {

                    $out_pix = imagecolorat($src_image,$pix_x,$pix_y);
                    $colors = imagecolorsforindex($y_image, $out_pix);
                    //$src_pix_array = rgb_to_array($src_pix);

                    if($colors['red'] == 0 && $colors['green'] == 0 && $colors['blue'] == 0) $alpha = 127;
                    else $alpha = 80;

                    imagesetpixel($y_image, $pix_x, $pix_y, imagecolorallocatealpha($y_image, $colors['red'], $colors['green'], $colors['blue'], $alpha));


                }//end for

            }//end for

            $startY++;
            $endY++;

            imagepng($y_image,$x_directory . '/' . $y . '.png') or die('Problem saving image: ' . $x_directory . '/' . $y . '.png');
            imagedestroy($y_image);
            //imagedestroy($src_image);

        }//end if
        else 
        {
            $black = imagecolorallocate($y_image, 0, 0, 0);
            // Make the background transparent
            imagecolortransparent($y_image, $black);

            imagepng($y_image,$x_directory . '/' . $y . '.png') or die('Problem saving image: ' . $x_directory . '/' . $y . '.png');
            imagedestroy($y_image);

        }

        $time = time() - $time;
        ?>
        Image <?=$x?>,<?=$y?> time: <?=$time?> Seconds <br/> 
        <?

    }
    //end for y

    if(($x >= 2 && $x <= 5)) {
    $startX++;
    $endX++;
    }

}//end for x
  • 写回答

1条回答 默认 最新

  • dongxia1390 2014-04-11 17:03
    关注

    If you're trying to split the big image into smaller images, i suggest you just call imagecreatefrompng() once, then create images with imagecreatetruecolor() and use imagecopyresampled() for each of the bits copying in new image objects (or use same one and save the parts after you extract them)

    Here is how i would do it:

    $main_image = imagecreatefrompng($path); //assuming you know the path
    //do your for's and every time your loop runs, you use an $part_image like this
    
    //LOOP START
    $part_image = imagecreatetruecolor(you know your parameters, atleast you should);
    //do the imagecopyresampled with your coordinates 
    imagejpeg($part_image, $part_path); //save the image, make sure each time you do a different filename in order not to overwrite the first part - maybe increment a $counter variable and append it to the filename like "part1.jpg" and so on
    //or imagepng, see php docs for these functions
    imagedestroy($part_image);
    //LOOP END
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料