使用imagejpeg循环创建多个图像

时间:2018-05-23 17:09:50

标签: php loops foreach

我构建下面的循环以使用现有图像生成多个图像,并从用户输入的表单字段中收集文本。其中一个变量是通过解析带有名称的csv文件而生成的数组。

我遇到的问题是代码只输出第一张图片,而不是其他任何图片。该数组有9个值,但该数字可以根据用户的csv进行更改。

<?php
$school = $_POST["school"];
$educator1 = $_POST["educator1"];
$educator2 = $_POST["educator2"];     
$students = $_FILES["csv"]["tmp_name"];
$csvAsArray = array_map("str_getcsv", file($students));

foreach ($csvAsArray as $value) {

    //Set the Content Type
    header('Content-type: image/jpeg');

    // Create Image From Existing File
    $jpg_image = imagecreatefromjpeg('certificate.jpg');

    // Allocate A Color For The Text
    $black = imagecolorallocate($jpg_image, 0, 0, 0);
    $grey = imagecolorallocate($jpg_image, 63, 63, 63);

    // Set Path to Font File
    $font_path = 'GothamRounded-Bold.otf';

    // Print Text On Image
    imagettftext($jpg_image, 90, 0, 90, 920, $black, $font_path, $value[0]);
    imagettftext($jpg_image, 50, 0, 850, 1120, $black, $font_path, $school);
    imagettftext($jpg_image, 50, 0, 90, 1480, $grey, $font_path, $educator1);
    imagettftext($jpg_image, 50, 0, 1050, 1475, $grey, $font_path, $educator2);

    // Send Image to Browser
    // $name = $value[0] .'.jpg';
    imagejpeg($jpg_image);   

    // Clear Memory
    imagedestroy($jpg_image);
}

我没有直接推送到浏览器,而是尝试使用$ name变量保存图像文件以创建以下内容,但这也不起作用。

$name = $value[0] .'.jpg';    
imagejpeg($jpg_image, $name);

0 个答案:

没有答案