Streaming file to laravel storage - laravel

I am using JPGraph to generate a graph, the hope is to then save the graph to the file system and point to it in a laravel view. I am attempting to render that view into a PDF later (this is why I'm not generating the chart on the fly).
The problem I am having is I can't figure out how to get the Stream method in JPGraph to save to the Laravel file system.
$graph = new Graph\PieGraph(350, 250);
$graph->title->Set("A Simple Pie Plot");
$graph->SetBox(true);
$legends = ['A (%d)','B (%d)','C (%d)','D (%d)','E (%d)',];
$data = array(40, 21, 17, 14, 23);
$p1 = new Plot\PiePlot($data);
$p1->SetLegends($legends);
$p1->ShowBorder();
$p1->SetColor('black');
$p1->SetSliceColors(array('#1E90FF', '#2E8B57', '#ADFF2F', '#DC143C', '#BA55D3'));
$graph->legend->SetPos(0.5,0.98,'center','bottom');
$graph->Add($p1);
$image = $graph->img->Stream(/* TO LARAVEL STORAGE */);
I have tried pointing directly to the path using storage_path('images\graphs\test.png'); but I get a permissions error.

Related

Error retrieving image from URL with SpreadsheetApp.newCellImage() builder

My application is trying to insert images to google drive sheet using google app script.
It works fine...
but it hang up intermittently with the response error from google script:
Exception: Error retrieving image from URL or bad URL:
https://drive.google.com/uc?id=1uvjg9_ZZg2sI5RYbPMEn6xXJhhnwuFyq&export=download.
The code is :
var fpm_mon_image = file_from_folder(id_folder_images , image_AR ) ; // get "image_AR" from folder
var url_mon_image = fpm_mon_image.getDownloadUrl() ;
var image = SpreadsheetApp.newCellImage().setSourceUrl(url_mon_image).setAltTextTitle(titre).toBuilder().build() ;
Utilities.sleep(1000); // for testing ...
SpreadsheetApp.flush();
Utilities.sleep(1000);
var rangeIma= fpm_sp.getRange(scal_li_SP +1, 2) ;
rangeIma.setValue(image).setVerticalAlignment('bottom') ; // stop here with error above
It works fine 5, 10 times then it hang up 2, 3, 5 times and then works fine again.... (I start loosing my hairs ;-))
I tried :
var srcfile = DriveApp.getFileById(id_mon_image);
srcfile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
var image = fpm_sp.insertImage(srcfile.getBlob(), my_col , my_row);
but the image in not inserted in a cell...
Could you help please ?
Many thanks.
Unfortunately, I cannot replicate your situation of Exception: Error retrieving image from URL or bad URL:. So, although I'm not sure about your actual situation, how about the following modification?
Modified script:
Before you use this script, please enable Drive API at Advanced Google services.
var fileId = "###"; // Please set the file ID of your image.
var url = Drive.Files.get(fileId).thumbnailLink.replace(/\=s.+/, "=s512");
var title = "sample title";
var image = SpreadsheetApp.newCellImage().setSourceUrl(url).setAltTextTitle(title).build();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").setValue(image);
When this script is run, a thumbnail link is retrieved from the image file using Drive API. And, the image size of the image is changed. And, the image is put to the cell "A1" of the active sheet.
In this case, if the issue of image size occurs, this issue can be removed. And, if the issue of sharing the file occurs, this issue can be also removed. By this, I thought that your issue might be able to be removed.

Telegram's instant view: fails to load .webp and .ico images

I have a problem when trying to create my Telegram's Instant View template, with this error:
Resource fetch failed: https://gdude.de/blog/assets/images/Kaggle-Lyft/task.webp
Resource fetch failed: https://gdude.de/blog/assets/images/telegram.ico
The URLs are valid, I have checked.
These are the only two images that fail. Does IV support *.webp and *.ico images?
According to their manual, Instant View is only actually supporting gif, jpg and png.
with the attribute src and the optional attribute href to make the image clickable. Allowed formats: GIF, JPG, PNG (GIF would be converted into Video type by IV)
I had a similar problem and solved it in the following way.
Note: You need a hosting server to store a PHP script, a free one worked for me (000Webhost).
The diagram below represents the general idea
Instant View code
Note: I'm a beginner at Instant View and XPath, so for now I'm just writing code that works.
# Find unsupported images
$imgs: //img[ends-with(#src, ".webp")]
$imgs+: //img[ends-with(#src, ".ico")]
# Temporary element to create the URLs and make calls to the conversion service
#html_to_dom: "<a>"
$tmp_tag
# For each unsupported image
#map($imgs){
$img: $#
# Build de URL to request the image conversion service
#set_attr(href, "https://my-free-subdom.000webhostapp.com/imgconverter.php?url=", $img/#src): $tmp_tag/a
# Make the request
#load: $tmp_tag/a/#href
# Change the src of the unsupported image to that of the converted image created by the conversion service
#set_attr(src, $#//img/#src): $img
}
#remove: $tmp_tag
PHP script to convert the image
To handle the ICO files I used the IcoFileLoader library, I found it thanks to this question PHP GD .ico handling. I just took the PHP files from the src directory and put them directly on my hosting, so I had to use a simple Autoloader.
// The URL of the image to convert
// If the url of the image is relative, you have
// to build it here, example $url = 'https://gdude.de'.$_GET['url'];
$url = $_GET['url'];
// File name
$file_name = basename($url);
// Directory where the image will be saved
$dir = './';
// File location
$save_file_loc = $dir . $file_name;
// Open file
$fp = fopen($save_file_loc, 'wb');
// Download the image using CURL
$ch = curl_init($url);
// Set options for a cURL transfer
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
// Close file
fclose($fp);
// Load the image
// ICO images need special handling
if(str_ends_with($file_name, '.ico'))
{
require_once('Autoloader.php');
$loader = new IcoFileService;
// You must define the size, I did the tests with a 16X16 favicon.
$im = $loader->extractIcon($file_name, 16, 16);
}
else if(str_ends_with($file_name, '.webp'))
{
$im = imagecreatefromwebp($file_name);
}
// Check if the image was loaded
if(!isset($im))
{
die('Unable to load image!');
}
// Convert it to a png file
imagepng($im, $file_name.'.png');
imagedestroy($im);
// Delte the original image
unlink($file_name);
// "Return" the image in an HTML tag so that Instant View can handle it
echo '<img src="https://my-free-subdom.000webhostapp.com/' . $file_name . '.png">';
Feel free to improve the above code, perhaps add some security, delete old images, use other libraries or PHP functions, accept multiple images in the same request, etc.
I found imageoptim to be helpful for free conversion of (in my case) svg images. Just prepend this url to the svg url and they'll start to load. I chose a resolution of 2560 as it's the max resolution that IV 2.1 supports.
#set_attr(src, "https://img.gs/<your-username>/2560,fit,format=png,quality=high/", ./#src): $body//img[contains(#src, ".svg")]
#set_attr(srcset, "https://img.gs/<your-username>/2560,fit,format=png,quality=high/", ./#srcset): $body//img[contains(#srcset, ".svg")]

Deleting images in Google sheet via script error

I probably will get downvoted for this but I can't do anything else. I searched, read and tried SOOOO many things. I am new at both Javascript and Google platform.
I have this piece of code that keeps telling me
"Missing name after . operation".
Even though .delete and .getAltTextTitle are both shown in autocomplete suggestion
var images = sheet.getImages();
var img = images[0];
Logger.log(img.getAltTextTitle()); //This is fine
img.delete(); //This throw error
In the meanwhile, I have this piece of code working totally fine. I don't know what to do. They look the same to me.
var img = sheet.getImages()[0];
var imgH = img.getHeight();
var imgW = img.getWidth();
var maxW = 700;
var newImgH = Math.round(imgH*(maxW/imgW));
img.setWidth(maxW);
img.setHeight(newImgH);
Unrelated topic, where do I get the documentation for sheet.getImages() because I just couldn't find it here
You want to delete the image on Spreadsheet using Google Apps Script.
If my understand is correct, I think that delete() might be new method which will be added for Spreadsheet in the near future. So the document is not updated and it might not complete yet. But from the error message, if you want to use the current delete() method, how about this sample script?
Sample script:
var images = sheet.getImages();
var img = images[0];
img["delete"](); // this one
Note:
I think that the detail infomation might be added to this document.
In my environment, I could confirm that the image can be deleted by img["delete"](). I think that this is one of new methods.
If I misunderstand your question, I'm sorry.
Update at 2018 October 13:
Now in order to delete the image, the following method can be used. This was confirmed at 2018 October 13.
var images = sheet.getImages();
var img = images[0];
img.remove(); // Here
Update at 2018 October 31:
This was officially released at October 30, 2018.
You can see the document at Class OverGridImage.

Load blob image in fpdf

I am trying to load an image from oracle database Blob file. To load image i am using the following code
$COM_CODE = 'O003';
$sql = 'select DIGI_SIGN from LC_BLOCK_LIST_TECH_PERS where COM_CODE :COM_CODE';
$s = oci_parse ($c, $sql);
oci_bind_by_name($s, ':COM_CODE', $COM_CODE);
oci_execute($s, OCI_NO_AUTO_COMMIT);
$arr = oci_fetch_assoc($s);
$result = $arr['DIGI_SIGN']->load();
$pdf->Cell(40,5,$result,0,1,'L',0);
Nedd to show the image in a cell. In last cell i am not able to show the image instead binary value shows up.
what am i doing wrong here?
from here, it looks like you need to first create a file, then use standard procedure to write the image to the pdf:
$result = $arr['DIGI_SIGN']->load();
// Here: new question: [php] how to write image file from Oracle BLOB?
// $result goes to a file on server: "/var/appli/img/products/image1.jpg"
$image1 = "/var/appli/img/products/image1.jpg";
$pdf->Cell( 40, 5, $pdf->Image($image1, $pdf->GetX(), $pdf->GetY(), 30), 0, 1, 'L', 0);
Hope it helps.

FPDF Error - Can't get image to appaear on PDF

I have a PDF with fillable forms. I can successfully fill the forms and save the new PDF to my server no problem, but I cannot place an image on the PDF. When I remove the Image(....) line the script works great. When I add it back it I get the error.
I think it's trying to look for the method in the wrong file, originally I only included fpdm..php but I tried adding back fpdf.php and it did not help. Not sure what I'm doing wrong.
Error:
Fatal error: Call to undefined method FPDM::Image() in /home/.../formPDF.php on line 113
Code:
require('../forms/pdf/fpdf.php');
require('../forms/pdf/fpdm.php');
$pdf = new FPDM($formPDFLocation);
$pdf->Image('images/sig_37-1405313221.png', 100, 20);
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$filename= "../forms/generated/" . $ffID;
$pdf->Output($filename.'.pdf','I');
FPDM is not really related to FPDF but simply a script of a FPDF user. You cannot use FPDF methods on an FPDM instance.
If you search for an all in one solution for such task you may take a look at the SetaPDF-FormFiller component (not free!).
I was able to save the PDF to my server and then add the image but lost the form contents. Code for anyone that is interested in this half solution:
Code for adding image:
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile("16.pdf");
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->Image('car.jpg',20,100);
$pdf->Output();
?>

Resources