I used barcode generation in my site. When I used it within HTML tags it’s working fine.
<div class="barcode_img"><img src="<?php echo AT::getUrl(); ?>/barcode/image.php?code=code39&o=1&dpi=150&t=30&r=1&rot=0&text=TEST NAME WITH SPACE&f1=Arial.ttf&f2=10&a1=&a2=&a3=" class="barcode fr"/></div>
I need to get that image to store it in local folder called “media/barcode/”. For that, I used the code below:
$valid_barcodename="testimage";
$barcodeurl = AT::getUrl() . "barcode/image.php?code=code39&o=1&dpi=150&t=30&r=1&rot=0&text=TEST NAME WITH SPACE&f1=Arial.ttf&f2=10&a1=&a2=&a3=";
$barcode_img = 'media/barcode/testing_' .$valid_barcodename . '.png';
file_put_contents($barcode_img, file_get_contents($barcodeurl));
The image stored in that folder is not empty. When I analyzed it I found, if I give the name “TEST NAME WITH SPACE” without space (TESTNAMEWITHSPACE), it works.
However if I give it with space it won’t work. What is the issue?
Note: AT::getUrl() – used for get my base url.
Spaces have special meaning in URLs, so you have to encode them:
In the above code,
$textnow contains your text, encoded and ready to be used in your URL (you will notice the spaces have been replaced with%20codes).