here I have to cache about 2000 favicon.ico files for performance enhancements. I grab the files and try to shrink them via IMagick (v.6.6.0) and PHP 5.3.5
The PHP code for this is
try {
$image = new Imagick($im_hint . ':' . BASE . '/upload/favicon.ico');
$image->cropThumbnailImage(16, 16);
$image->setImageFormat('ico');
$image->writeImage(BASE . '/favicons/' . $id[0] . '/' . $id[1] . '/' . $id[2] . '/' . $id . '.ico');
} catch (Exception $e) { die($e->getMessage()); }
where $im_hint could be ico, png, jpg and so on.
For 99% of the files all is fine and I get a working ICO file. But for one percent of files, I get only a blank ICO file and I don't know why? An example for an ICO file where this code fails is http://www.augensound.de/favicon.ico
I tried to comment out the cropThumbnailImage call and try to use setFormat instead of setImageFormat and tried to save it as PNG...but nothing works. There is also no exception.
Regards
Not an answer to your question, but I get a blank image too when I open the example file in IrfanView or PhotoImpact. It's not a multi-page/multi-resolution file so there is nothing to switch. The canvas just is blank.
Windows 7's built-in preview renders it fine, though.
It could be that IM can't deal with these files because they have the wrong format or sub-format.
Related
I have tried to send image and video file as response with the code as per CakePHP 4 documentation: https://book.cakephp.org/4/en/controllers/request-response.html#sending-files
Below is my function to access file from url.
public function noteFileAccess() {
$filename = $this->request->getParam('filename');
$id = $this->request->getParam('id');
$path = Configure::read('FilePath.NoteFiles');
$file = new File($path . $id . DS . $filename);
if(!$file->exists()) {
throw new ForbiddenException;
}
$this->response = $this->response->withFile($file->path);
//Also tried to set mime type like below commented line
//$this->response = $this->response->withType($file->mime())->withFile($file->path);
return $this->response;
}
By using above code, it is sending pdf, zip, csv files properly. But images & videos files are not working. Its showing white square in chrome browser. And if downloaded it is showing format not supported in windows system.
Only different I see in downloaded image file vs original image file is image dimensions and some other things like in screenshot below
Edit: File Content Difference added below
I have checked with file path & mime type of the image file is good.
Do you think I am missing anything here?
Okay, so the problem is essentially: convert an svg to a pdf using image magick.
I take an image in a base64 format, do something to it, convert it to a bitmap, do something to is, store it inside an SVG string, write the svg to a file (doesn't have to be done, but wanted to make sure the svg was loading), then from here I need to convert the svg to a pdf.
//I just broke the string up over spaces for sake of readability
$svgStr = "<?xml version=1.0.' encoding='utf-8' standalone='no'?>
<svg xmlns="http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' xml:space='preserve'>
<desc>
Made by Shane
</desc>
<defs>
</defs>
<style>
</style>
<image width='500' height'auto' xlink:href'" . $path . $filename . $ext . "' />
</svg>";
file_put_contents($path . $svgFile . ".svg", $svgStr);
//Attempt 1 of many
$pdf = New Imagick($path . $svgFile . ".svg", "SVG");
$pdf->setImageFormat("PDF"); //This thows the "Can not process empty imagick object" error.
file_put_contents($path . $svgFile . ".pdf", $pdf);
//Attempt2 of many
$pdf = New Imagick();
//Throws an error here - can not process empty Imagick object...
//which brings me back to attempt 1... the idea in attempt 1 is to initialize
//the Imagick object with the svg format and data, then convert.
//Here the idea is to initialize it step by step
$pdf->setImageFormat("SVG");
//For readImageBlob(), I should be able to read the svg file or use the $svgStr.
$pdf->readImageBlob($svgStr);
$pdf->setImageFormat("PDF");
file_put_contents($path . $svgFile . ".pdf", $pdf);
I am running image magick 6.8.9-9.
I can't upgrade, because I don't have sudo and it can't be accessed is the long story short.
EDIT:
I think this might be part of the problem - I get an error converting a .bmp to a pdf from the command line:
https://askubuntu.com/questions/1081695/error-during-converting-jpg-to-pdf
However, I don't know if the command line utilities operate in the same manner as the class wrapper does...
Please help 0_0
Thank you.
I am using QRICKIT (qrickit.com) to generate QR codes. Instead of generating codes every time the page refreshes, I want to download them and use the ones already generated.
So, within the loop that iterates through my database table, I do this:
$qrcodebase = "https://qrickit.com/api/qr.php?d=";
$filename = "./product_images/" . $row["itemID"] . "_QR.png";
if (!(file_exists($filename))) {
$url = $qrcodebase . $myurl . $brandpage . "/?itemID=" . $row["itemID"];
file_put_contents($filename, file_get_contents($url));
}
echo "<td><img src=\"" . $filename . "\" height=60 width=60></td>";
When this executes, it generates the right HTML to display the image, and when I FTP to the product_images folder, the QR codes are there. When I download the QR codes and open them up in an image editor, they open just fine. But, when the web page displays, I get an X in a box that indicates a bad image. Does anyone have any idea why?
Bit of a weird issue.
I'm generating some HTML reports and then, due to Outlook 2010's limitations of understanding CSS/HTML/Images, I'm sending the whole damn report out as an image.
The generation works fine. Previewing the generated HTML in a browser looks lovely at a range of resolutions. However, WKHTMLTOPDF (or rather, wkhtmltoimage) is giving me some odd behaviour with it's --width flag.
I have the following PHP code:
# Converts a .html file to a .jpg file - requires wkhtmltopdf installed:
# http://wkhtmltopdf.org/downloads.html
function html_to_jpg($fileIn, $fileOut) {
echo "\nRendering HTML to image\n";
$command = 'wkhtmltoimage --width 1130 ' . $fileIn . ' ' . $fileOut;
$remove = 'rm -f ' . filter_var($fileOut, FILTER_SANITIZE_STRING);
try {
exec($remove);
exec($command);
return true;
} catch (Exception $e) {
return false;
}
}
This works. It generates an image of "1130" width as specified. However, the report looks a little dodgy at that width, so I increase the --width...
$command = 'wkhtmltoimage --width 1140 ' . $fileIn . ' ' . $fileOut;
Now this still generates an image, however, the image is now 36480 pixels wide, with the content centered, and a stack of black space on the right.
Through experimentation I've confirmed that widths < 1140 do not generate this problem, but every width above does. I've confirmed this on Ubuntu 15.10 Wily, and CentOS 6.7, both installed from different RPMs.
Has anyone else experienced this? Am I doing something stupid (other than using exec)?
I have experienced the same behavior, and I fixed it by changing the width of the html element to match the width that I pass in the command and it works fine
so basically in your $fileIn add this in a style tag
html, body {
width:1140px !important;
}
of course you can ommit the !important if nothing else affects the width of these elements in your file.
I wrote a quick PHP script for loading an image in a directory, but I've been finding that it regularly gives me "Image truncated or corrupt" errors in the Error Console, despite the images not being corrupt. I'm able to view the image normally if I browse to its full path, and I'm also able to download it and view it without issue. I've tried using different browsers but have the same problem.
Oddly enough, some images work fine and some do not. The issue doesn't seem to be related to file size.
My code is pretty simple, and I've been using it for ages:
if (isset($_GET['i']) && is_numeric($_GET['i'])) {
$path = 'D:/Images/';
if (is_file($path . $i . '.jpg')) {
header('content-type: image/jpg');
require($path . $i . '.jpg');
}
You shouldn't be using require, that includes the image as if it were PHP code. You should be using readfile(), which will just open the image and send it straight to the browser:
readfile($path . $i . '.jpg');
exit;
Also note that you'll want to call exit() after readfile().