Rename a base64 encoded image on download - image

I have the following code to display an image - without going into details about why I am not just using the image name.
<?php
$data = base64_encode(file_get_contents('example.png'));
$info = getimagesize('example.png');
$image = sprintf('data:%s;base64,%s', $info['mime'], $data);
?>
<img src="<?php echo $image; ?>" download='final-image.png' />
It works as expected displaying the image.
However when someone right-clicks the image, in FireFox it wants to save it as (scriptname).png and Chrome wants to save it as download.png.
I can't use header() as headers are already being sent.
Is there any way to force a filename that works across browsers?

Related

Qrcode in laravel

i want to make qrcode, in there i can make it but i have something trouble when i want to change the format of qrcode to png file. but its only show symbol
here my view :
<?php echo QrCode::size(265)->generate($row->id) ?>
this qrcode i use :
"simplesoftwareio/simple-qrcode": "~1"
here my referance : https://www.simplesoftware.io/docs/simple-qrcode
can anyone help me ? or can someone give me solution ?
before i change format to png :
and this after i change it :
There are more simple example available as well.
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(100)->generate('QrCode as PNG image!')) !!} ">
If you are formatting it as png file format, you need to include it with a <img> tag.
Taken from the documentation
//Inside of a blade template.
<img src="{!!$message->embedData(QrCode::format('png')->generate('Embed me into an e-mail!'), 'QrCode.png', 'image/png')!!}">
You can also do this:
$png = QrCode::format('png')->size(512)->generate(1);
$png = base64_encode($png);
echo "<img src='data:image/png;base64," . $png . "'>";
In controller
$path = getenv('IMAGE_URL')."/img/logo.png";
$png = QrCode::format('png')->merge($path, .17, true)->size(300)->errorCorrection('H')->generate($data);
$png = base64_encode($png);
In blade file
<img src='data:image/png;base64,{{$png}}'>
I came to this page because I needed to create a PNG file to send inline in an email in laravel8.
I used the examples above in order to create the QR code as a png which worked brilliantly. I used the following code:
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(100)->generate('QrCode as PNG image!')) !!}" />
However a number of email services (including Gmail) do not display images that use inlined base64 as above. Instead, they require you to add the base64 image as an attachment to the email and then reference that attachment in the img src.
Thankfully laravel (I'm using laravel8) has a really cool function that does this for you, so my code ended up looking like this (which worked):
<?php
$qrCodeAsPng = QrCode::format('png')->size(500)->generate("my text for the QR code");
?>
<img src="{{ $message->embedData($qrCodeAsPng, 'nameForAttachment.png') }}" />
The $message variable is one that is in every blade that is being sent as an email in laravel. In my case I did not want to create an actual image on the server, but if you wanted to use an image that you had stored you would use $message->embed().

Embed Twitter pics as a collage for a website

I'm trying to create a collage of images that are sourced from a twitter feed to be displayed in a website banner.
So far I have set up the Twitter authentication
I'm not sure how to go about extracting just the images from her feed, or embed them in the desired manner.
Any help or information would be greatly appreciated.
Click here to view a possible outcome I'm looking for.
Thanks!
To simply dump a couple of images, try this:
<?php
require_once("/twitteroauth/twitteroauth.php");
$consumerKey = "0LQwV6gQi1hi8TPOMvfqy7qpM";
$consumerSecret = "AuShkWcg7lj8VYyKYOH6WPW8tY6B2QuB8kgRpmALVrg0VK0mM0";
$accessToken = "624631261-DjWeq4RB0eGAB2lr68skZE1zbfDcg2x6BEyJzFZO";
$accessTokenSecret = "8XnWnkiFQ803r9sHVkDrZNhD50EuJlnw8sZI2EPsA9p7X";
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%rhyareegaming%20filter%3Aimages&include_entities=true");
foreach ($tweets->statuses as $status) {
?>
<img src="<?php echo $status->entities->media[0]->media_url; ?>">
<?php
}

display no image if doesnt exist codeigniter

All i want to do is display an image if the image exist if not display blank but i cant seem to figue it out.
Im storing my image path file in my database, in the column called, file and path is stored like this:
/images/file_name
now if there isnt an image being posted it stores /images/
if ($news_item['file'] = '/images/11.jpg') { ?>
<img src="<?php echo base_url(); echo $news_item['file'] ?>">
} else {
echo '';
}
?>
If you need anything more of me let me know but just to give you an idea of what I'm achieving.
its a news post, if you create a post you have a title and description and you have the choice of entering in a file/image.. if you leave it blank it will enter in /images/ and if you enter an image in it will be: /images/file_name (this is inserted into a db)
Check like this:
if (file_exists($news_item['file'])) {
I would simply use the alt property of the image to show an alternative text:
<img src="<?php echo base_url(); echo $news_item['file'] ?>" alt="no image">
or, if you want it to be completely blank:
<img src="<?php echo base_url(); echo $news_item['file'] ?>" alt="">
EDIT:
To completely remove the image, you can conditionally generate the image or an alternative text:
if (file_exists($news_item['file'])) {
echo '<img src="'.base_url().$news_item['file'].'">';
} else {
echo "No image";
}
I suggest you to store the default.jpg in DB ( You can set this default in DB ). And store the default.jpg image in image folder. So that you don't need to change anything in coding.

how to open pdf file to another tab in browser using codeigniter

im making currently making my thesis about a record management of our university secretary.. in which all papers inside the office will be scanned and uploaded in the system.. i am using codeigniter..one of the feature in my system is to view the pdf file in other window of the browser. but my problem is, when i click the title. only blank page will be displayed in the other tab.. can you help me solve this one?? here is my code
controller:
function viewMinutesFile(){
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = $this->minutes_model->getFile($id);
$fp= fopen($file->path, "r");
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$file->filename."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' .filesize($file->path));
ob_clean();
flush();
while (!feof($fp)){
$buff = fread($fp,1024);
print $buff;
}
exit;
}
}
code to open the file: this is my syntax to be clicked by the user so that pdf file will be open in the new tab
File
index.php/admin/viewMinutesFile?
id=" target="_tab">
try this one with a static url. no need any extra words for that.
Show My Pdf
New Update
if its work for you then fetch pdf name from database and put the name in the view like
Show My Pdf
now in the controller
$this->load->helper('download');
if($this->uri->segment(3))
{
$data = file_get_contents('./file_path/'.$this->uri->segment(3));
}
$name = $this->uri->segment(3);
force_download($name, $data);
well, you could add a link to file with target="_blank", like
<a href="<?php echo base_url(). 'your_controller/viewMinutesFile'; ?>" target="_blank">
View Pdf
</a>
and in controller function:
function viewMinutesFile(){
....
$file = $this->minutes_model->getFile($id);
$this->output
->set_content_type('application/pdf')
->set_output(file_get_contents($your_pdf_file));
}
you can try this on your view :
Filename
and on your controller, you can try this, because this is works for me :
function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploaddir/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
hope this might help you...
Just create a link to a blank page and use this code in your controller:
public function myPdfPage(){
$url = base_url('assets/your.pdf');
$html = '<iframe src="'.$url.'" style="border:none; width: 100%; height: 100%"></iframe>';
echo $html;
}
Enjoy!
There is no any problem with your code you can open easily on next tab, like other pages only difference you have to change header description and it is make sure on your browser pdf reader add-ons are available, otherwise it will give you option to download.
You may just follow this.
<?php echo form_open_multipart('your_controller/your_function','target="_blank"') ;?>
//other input fields
<?php form_close();?>

Magento custom template - edit lightbox sizing

I am using a custom magento template which has a lightbox on the product page. I can set the max width/height for the lightbox in the config as a percentage of the screen (ie 95%). The problem is it then uses this as the default value so even it is is a really small image it will stretch it out to 95% of the screen and cause the image to pixelate.
I would like it to use this percentage as a maximum only but if the actual image is smaller than this then it would use the actual image dimensions.
I have found a file with this code which I think I may need to edit but I am not sure how to go about this in Magento:
<?php
$maxWidth = $zoomHelper->getCfg('lightbox/max_width');
$maxHeight = $zoomHelper->getCfg('lightbox/max_height');
$cfg = '';
if ($maxWidth)
$cfg .= ", maxWidth:'{$maxWidth}'";
if ($maxHeight)
$cfg .= ", maxHeight:'{$maxHeight}'";
?>
Any ideas?
The way I've done this is to load up relatively large images as the base product image. When the product / view is rendering, build a js object that contains urls for large images, and any thumbnails you need. Your lightbox should then use the js object as a data source.
media.phtml
<script> var images = {};</script>
<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image){ $i++; ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(108,90); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" /><?php } ?>
<script> images.bigsrc = "<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(800,800); ?>" </script>
<?php } ?>
You then have a js object containing urls for whatever size image you would need. Pass that into lightbox / fancybox etc.
You will definitely have to tailor this to match your needs - mostly in terms of creating a js object that you can use.
Some info on resizing images in mage:
http://www.magthemes.com/magento-blog/customize-magentos-image-resize-functionality/

Resources