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


How to save barcode image to folder in drive of computer?

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

HTML

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

我如何保存这个条码图像到我的本地驱动器硬盘驱动器?

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

$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);

希望能有所帮助