如何将条形码图像保存到计算机驱动器中的文件夹?

时间:2014-06-06 07:10:07

标签: php

我使用PHP条形码插件创建了一个条形码

HTML

<img src="barcode.php?codetype=Code128&size=50&text=123421321321321321" style="width:auto; height:auto;" alt="123456789"/>

如何将此条形码图像保存到本地硬盘驱动器?

1 个答案:

答案 0 :(得分:-1)

试试这个获取内容并将其保存到文件

$barcode = file_get_contents('http://www.youdomain.com/barcode.php?codetype=Code128&size=50&text=123421321321321321');

//you can also use below function get more info on image 
//$imgInfo = getimagesize($barcode);

//save the file to disk 
file_put_contents('path_to_file/code.png', $barcode);

使用CURL

//You can also try it using CURL if above doesn't works

$ch = curl_init('http://www.youdomain.com/barcode.php?codetype=Code128&size=50&text=123421321321321321');
$fp = fopen('path_to_file/code.png', 'wb');
      curl_setopt($ch, CURLOPT_FILE, $fp);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_exec($ch);
      curl_close($ch);
fclose($fp);

编辑barcode.php

现在非常简单,就像在文件中简单地返回图像标题一样,就在你可以添加这一行以将其保存在服务器上之前

// Save the image to file.png
imagepng($image, "file.png"); // Your barcode will be saved here with name file.png

// Draw barcode to the screen
header ('Content-type: image/png');
imagepng($image);
imagedestroy($image);

希望这有帮助