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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么