Caching images using PHP Image Cache - php

I am using this image caching library (http://nielse63.github.io/php-image-cache/) to cache images I am getting using the OMBD API (http://omdbapi.com/).
This is my setup:
$imagecache = new ImageCache();
$imagecache->cached_image_directory = '/img/cache/';
$imagecache->cached_image_url = '/img/cache';
$cached_src_two = $imagecache->cache( 'http://image.tmdb.org/t/p/original/eYVZP9CibJg1fyZnZahfFIYqfPr.jpg' );
echo 'Original file size: ' . filesize($imagecache->image_src) . ' bytes<br>';
echo 'PHPImageCach-ified file size: ' . filesize($imagecache->cached_filename) . ' bytes<br>';
echo 'Total image size reduction: ' . (((filesize($imagecache->image_src) - filesize($imagecache->cached_filename)) / filesize($imagecache->image_src))*100) . '%';
echo '<img src="'.$cached_src_two.'" alt="">';
I am not getting any errors. But something weird is going on I can't understand. It is saving a local copy of the image in $imagecache->cached_image_directory - this is not compressed or cached. Then it is creating another file in the img directory.
I assume this is the cached file, but I am unable to open it to check. The cached filename looks like: eYVZP9CibJg1fyZnZahfFIYqfPr.jpg.jpeg
Note the jpg.jpeg. Even fixing the file extension does not work. I can't find anything useful in the documentation. What is going wrong here?

Related

How to generate thumbnail from PDF upload in laravel

I am trying to generate a thumbnail of the PDF I upload in laravel the thumbnail should be the first page of the PDF. Right now I am manually uploading an image to make the thumbnail like this:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
}
if (request()->has('cover')) {
$coveruploaded = request()->file('cover');
$covername = $request->book_name . time() . '.' . $coveruploaded->getClientOriginalExtension();
$coverpath = public_path('/uploads/cover');
$coveruploaded->move($coverpath, $covername);
$book->card_image = '/uploads/cover/' . $covername;
}
This can be tedious while entering many data I want to generate thumbnail automatically. I searched many answers but I am not able to find laravel specific. I tried to use ImageMagic and Ghost script but I couldn't find a solution and proper role to implement.
Sorry, can't comment yet!
You can use spatie/pdf-to-image to parse the first page as image when file is uploaded and store it in your storage and save the link in your database.
First you need to have php-imagick and ghostscript installed and configured. For issues with ghostscript installation you can refer this. Then add the package composer require spatie/pdf-to-image.
As per your code sample:
if (request()->has('pdf')) {
$pdfuploaded = request()->file('pdf');
$pdfname = $request->book_name . time() . '.' . $pdfuploaded->getClientOriginalExtension();
$pdfpath = public_path('/uploads/pdf');
$pdfuploaded->move($pdfpath, $pdfname);
$book->book_file = '/uploads/pdf/' . $pdfname;
$pdf = $book->book_file;
$pdfO = new Spatie\PdfToImage\Pdf($pdfpath . '/' . $pdfname);
$thumbnailPath = public_path('/uploads/thumbnails');
$thumbnail = $pdfO->setPage(1)
->setOutputFormat('png')
->saveImage($thumbnailPath . '/' . 'YourFileName.png');
// This is where you save the cover path to your database.
}

Cakephp: Show image if uploaded or show text if image was not uploaded

I am developing a system where the user may upload up to four photos. If the user does not upload a photo I would like that the follow text will be displayed: 'empty'.
I have prepared the below code however I didn't managed :/ The problem is that when an image is uploaded it still prints 'empty' and the uploaded image does not show up.
<?php if (file_exists('../files/collection/photo2/' .$collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']))
{
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);}
else
{
echo ('empty');
}
?>
I appreciate you guidance and help :)
TLDR: The file you're checking for doesn't exist. Fix your path.
Detail:
You're trying to use the same path for both your HTML <img> as well as the PHP file_exists() check.
The problem is, that the HTML image is looking for a file via the user's browser, where the file_exists() method is looking for the file via your server. The two paths are very rarely the same.
Try using a correct path in your PHP's file_exists() method, and it should pass the check.
For example:
if(file_exists(APP . 'files' . DS . 'collection' . DS . 'photos2' . $collection['Collection']['photo_dir2'] . DS . 'thumb_' . $collection['Collection']['photo2'])) {
echo $this->Html->image('../files/collection/photo2/' . $collection['Collection']['photo_dir2'] . '/thumb_' . $collection['Collection']['photo2']);
}
else {
echo ('empty');
}

use file_exists() to check wordpress uploads folder

I have created a directory in Wordpress uploads folder for end user to bulk upload photos via ftp. Images are numbered 1.jpg, 2.jpg... etc. I've generated the image urls successfully, but now I want to test for empty urls - i.e. if "8.jpg" doesn't exist, show a placeholder image from the theme's images folder instead.
I'm trying to use file_exists(), but this returns false every time and always displays the placeholder image. Any suggestions would be appreciated.
<?php while ( have_posts() ) : the_post();
// create url to image in wordpress 'uploads/catalogue_images/$sale' folder
$upload_dir = wp_upload_dir();
$sub_dir = $wp_query->queried_object;
$image = get_field('file_number');
$image_url = $upload_dir['baseurl'] . "/catalogue_images/" . $sub_dir->name . "/" . $image . ".JPG"; ?>
<?php if(file_exists($image_url)){
echo '<img src="' . $image_url . '" alt="" />';
} else {
//placeholder
echo '<img src="' . get_bloginfo("template_url") . '/images/photo_unavailable.jpg" alt="" />';
} ?>
<?php endwhile; ?>
The PHP file_exists function mainly expects an internal server path to the file to be tested. This is made obvious with the example.
Fortunately, we see that wp_upload_dir() gives us several useful values:
'path' - base directory and sub directory or full path to upload directory.
'url' - base url and sub directory or absolute URL to upload directory.
'subdir' - sub directory if uploads use year/month folders option is on.
'basedir' - path without subdir.
'baseurl' - URL path without subdir.
'error' - set to false.
I've bolded what we want to use. Using these two values, you have to generate two variables, one for the external URL and one for the internal file path:
$image_relative_path = "/catalogue_images/" . $sub_dir->name . "/" . $image . ".JPG";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
Then use file_exists($image_path) instead of file_exists($image_url).
Note
As with the PHP notes on PHP >= 5.0.0, you can indeed use file_exists with some URLs, however the http:// protocol is not supported for the stat() function (which is what file_exists uses.)
You have to use an internal path for checking if a file exists.
So use $upload_dir['path'] instead $upload_dir['baseurl']
[path] - base directory and sub directory or full path to upload
directory.
http://codex.wordpress.org/Function_Reference/wp_upload_dir

Retrieve original uploaded filename using php on OpenShift

I would like to check that a file uploaded to my OpenShift app has a text extension (.txt or .tab). Following some advice given here I wrote the following code, with echoes added to help debug:
$AllowedExts = array('txt','tab');
echo "AllowedExts: " . $AllowedExts[0] . " and " . $AllowedExts[1] . "<br>";
$ThisPath = $_FILES['uploadedfile']['tmp_name'];
echo "ThisPath: " . $ThisPath . "<br>";
$ThisExt = pathinfo($ThisPath, PATHINFO_EXTENSION);
echo "ThisExt: " . $ThisExt . "<br>";
if(!in_array($ThisExt,$AllowedExts) ) {
$error = 'Uploaded file must end in .txt or .tab';
}
echo "error echo: " . $error . "<br>";
On uploading any file, the echoed response was:
AllowedExts: txt and tab
ThisPath: /var/lib/openshift/************/php/tmp/phpSmk2Ew
ThisExt:
error echo: Uploaded file must end in .txt or .tab
Does this mean that OpenShift is renaming the file upon upload? How do I get the original filename and then check its suffix? More generally, is there a better way to check the file type?
$_FILES['uploadedfile']['tmp_name'] contains the name of a temporary file on the server (which can be moved with move_uploaded_file()). If you want to check the original name of the uploaded file on the client machine use $_FILES['uploadedfile']['name'].
That's not an Open Shift issue, it's the standard way of PHP.
For further details see http://php.net/manual/en/features.file-upload.post-method.php
For other ways to detect the file type see http://php.net/manual/en/ref.fileinfo.php

MPDF - fseek error

I am trying to merge files together into one pdf. The files could be pdf, png or jpg files. Images work fine, its only when I try to export the pdf files that I get an error. The error is show below.
Message: fseek(): stream does not support seeking
I thought I found a solution from various forums relating to the path of the file. However, if I change the path it shows this error
mPDF error: Cannot open http://192.168.2.35/marine/certificate_files/a025ad3d40b22ac760ba7af7b6bb259d.pdf
My controller code is below
include('mpdf/mpdf.php');
$mpdf=new mPDF();
$mpdf->SetImportUse();
$mpdf->SetFooter($personnel_data->firstname . ' ' . $personnel_data->lastname . '|{PAGENO}|' . $personnel_data->ID_number );
foreach($certificate_data as $certificates)
{
$certificate_extension['type'] = explode('.',$certificates->certificate_name);
if($certificate_extension['type'][1]==='pdf')
{
$pagecount = #$mpdf->SetSourceFile('http://192.168.2.35/marine/certificate_files/' . $certificates->certificate_name);
$tplId = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplId);
$mpdf->WriteHTML('<pagebreak>');
}
if($certificate_extension['type'][1]!=='pdf')
{
$mpdf->WriteHTML('<p><img src="' . $this->config->base_url('assets/images/header-logo.png') . ' "></p>');
$mpdf->WriteHTML('<style>body {font-family: arial;}</style>');
$mpdf->WriteHTML('<p> ' . $certificates->certificate . ' - ' . $certificates->expiry_dates . '</p>');
$mpdf->WriteHTML('<p><img src="' . $this->config->base_url('certificate_files/' . $certificates->certificate_name) . ' "></p>');
$mpdf->WriteHTML('<pagebreak>');
}
else
{
$mpdf->WriteHTML('');
}
}
$mpdf->Output();
exit;
If any help or guidance could slide my way form anyone I would be forever grateful! Thanks!
as far as i know mpdf is based on fpdf (fpdi)... from the FPDI website: http://www.setasign.de/products/pdf-php-solutions/fpdi-pdf-parser/
By default FPDI can "only" handle PDF documents up to PDF version 1.4.
Beginning with PDF version 1.5 there were new compression features
introduced which involve internal structure changes how a PDF document
can be created.
Please try it with an PDF Version <= 1.4 for testing...
in my case it was the reason and I bought a developer license for the commercial pfdi pdf parser for 100€

Categories