-1

I've created a barcode using the PHP barcode plugin

HTML

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

How can I save this barcode image to my local drive hard drive?

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Toeur Tenh
  • 71
  • 4
  • 14

1 Answers1

-1

Try this get the content and save it to file

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

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

Edit barcode.php

Now its very simple, as in file its simply returning header of image, right before you can add this line to save it on server

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

Hope this helps

Saqueib
  • 3,484
  • 3
  • 33
  • 56
  • my purpose is want to save image automatic to disk, do not need alert message save box. Warning: file_get_contents(barcode.php?codetype=Code128&size=50&text=123421321321321321) [function.file-get-contents]: failed to open stream: No such file or directory in D:\wamp\www\SVN\roadTax\doc.php on line 12 – Toeur Tenh Jun 10 '14 at 01:35
  • path of barcode.php is not correct, try absolute path and it should work – Saqueib Jun 10 '14 at 03:59
  • I am sure path of barcode.php is correct because it's stay in the same root. – Toeur Tenh Jun 11 '14 at 01:45
  • use your absolute url of barcode.php like this `file_get_contents('http://www.youdomain.com/barcode.php?codetype=Code128&size=50&text=123421321321321321');` – Saqueib Jun 11 '14 at 04:39
  • I still get this error Warning: file_put_contents(http://localhost/SVN/roadTax/code.jpg) [function.file-put-contents]: failed to open stream: HTTP wrapper does not support writeable connections in D:\wamp\www\SVN\roadTax\doc.php on line 34. – Toeur Tenh Jun 12 '14 at 07:40
  • This is code: – Toeur Tenh Jun 12 '14 at 07:43
  • would you post the barcode.php in http://pastebin.com/ and send me the link, or share the barcode.php library link. – Saqueib Jun 12 '14 at 08:10
  • I am doing in local machine now. can how i share you the barcode.php? I think in this very difficult to share you. this is my skype: toeurtenh1. i want to share wity you by skype is easy then. – Toeur Tenh Jun 12 '14 at 08:18
  • Edit barcode.php? I am not well understand what you mean – Toeur Tenh Jun 12 '14 at 09:16
  • open barcode.php and add `imagepng($image, "file.png"); ` as shown in answer – Saqueib Jun 12 '14 at 09:23
  • editing barcode.php is simple, you just need to add one line of code and you are done. open it and scroll down just put `imagepng($image, "code.png");` before `// Draw barcode to the screen` that's all – Saqueib Jun 12 '14 at 09:28
  • ok already add imagepng($image, "code.png"); to barcode.php, how about this codes? $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); – Toeur Tenh Jun 12 '14 at 09:33
  • is it working, did you get a file in same folder named `code.png` – Saqueib Jun 12 '14 at 09:39
  • i get this error: Fatal error: Call to undefined function curl_init() in D:\wamp\www\SVN\roadTax\doc.php on line 16 – Toeur Tenh Jun 12 '14 at 10:23
  • this is my update code: $ch = curl_init('http://localhost/SVN/roadTax/barcode.php?codetype=Code128&size=50&text=123421321321321321'); $fp = fopen('http://localhost/SVN/roadTax/code.png', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); – Toeur Tenh Jun 12 '14 at 10:24
  • you dont have curl library installed. editing barcode.php is simplest way but i dont know why you are not listening – Saqueib Jun 12 '14 at 10:27
  • Ok now i get barcode image to my disk. thank you so much Mr. Saqueib. – Toeur Tenh Jun 13 '14 at 02:52
  • Can you help me one more problem? I can not post my question in here that why i want to ask you by comment: How can i conver div content to doc file in my disk? – Toeur Tenh Jun 13 '14 at 03:06
  • Currently i used PHP header like this: header("Content-type: application/octet-stream"); header("content-disposition: attachment;filename=FILENAME.doc"); but not auto save in to my directory disk. How can i do? – Toeur Tenh Jun 13 '14 at 03:09
  • you can use this library http://www.phpdocx.com/, please accept the answer if it solved. – Saqueib Jun 13 '14 at 04:44
  • it's not free for using – Toeur Tenh Jun 13 '14 at 05:02
  • try this http://www.phpkode.com/source/s/html-to-doc-classes/html-to-doc-classes/html_to_doc.inc.php – Saqueib Jun 13 '14 at 05:13
  • after run codes in http://www.phpkode.com/source/s/html-to-doc-classes/html-to-doc-classes/html_to_doc.inc.php, nothing happen , why? – Toeur Tenh Jun 13 '14 at 06:24