Home » Php » image – Convert RAW photos to JPEG in linux/php

image – Convert RAW photos to JPEG in linux/php

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m working on an photo upload app and need to allow users to upload raw files from cameras (these are not jpegs) and have the server automatically create a jpeg version of them. I currently have Imagemagick installed but I don’t think there is a way to do it in there.

Cameras come out with new raw formats all the time, so im looking for something that will be relativley up-to-date and command php exec() is an option too.

Does anyone have anything to suggestion for raw conversion?

How to&Answers:

Actually, as you can see in this list, ImageMagick does support RAW photos: http://www.imagemagick.org/script/formats.php (e.g. .NEF nikon photos and .CR2 canon photos).

Example code for a .CR2 photo:

$im = new Imagick( 'source.CR2' );
$im->setImageFormat( 'jpg' );
$im->writeImage( 'result.jpg' );
$im->clear();
$im->destroy();

Answer:

Even easier to
sudo apt-get install ufraw ufraw-batch

then either use ufraw (see man pages)
or use Imagemagic which uses ufraw

convert mypic.RAF mypic.jpeg

Answer:

You can upload with exec() and as you are converting to a jpg you can use the jpg “hint” to speed things up – it supposedly only reads as much data to create the jpg not the whole file.

From memory the define comes after the convert before the image:

convert -define jpeg:size=128x128 input.raw -thumbnail 128x128 output.jpg

Imagemagick uses Ufraw to work with RAW files and I find that with my CR2 files I need to tweek one of the files a bit. I suppose wether a file is supported depends on the Ufraw delagate.

I think Samsung RAW files are a problem but not just with Imagemagick

It is the delegates.xml file I modified changing a 4 to a 6 on this line:

<delegate decode="dng:decode" stealth="True" command="dcraw.exe -6 -w -O &quot;%u.ppm&quot; &quot;%i&quot;"/>

Answer:

I did the exact same page using Imagick. I was successful in converting TIFF, DNG, CR2 files into JPEG. Before you do this, you must refer to this video https://www.youtube.com/watch?v=q3c6O85_LoA&index=29&list=LLkmyV_KYAFxKz9gE_fEbjTA&t=23s

Here is a code with JavaScript, HTML and PHP:

<?php
$file=$_FILES['file'];
$fname=$_FILES['file']['name'];
$ftmp=$_FILES['file']['tmp_name'];
$ferr=$_FILES['file']['error'];
$check=explode(".",$fname);
$ext=end($check);
if(!$ftmp)
{
    //echo"ERROR: Please select a file first!";
    ?>
    <html>
        <script>
            alert("ERROR: Please select a file first!");
            location.href='/Image/1.html';
        </script>
    </html>
    <?php
}
else if(!preg_match("/\.(tif|tiff|cr2|pef|nef|dng)$/i",$fname))
{
    //echo"ERROR: File is not in TIF,TIFF,CR2,PEF,NEF or DNG</br>";
    //echo"File is of $ext format";
    ?>
    <html>
        <script>
            alert("ERROR: File is not in TIF,TIFF,CR2,PEF,NEF or DNG");
            location.href='/Image/1.html';
        </script>
    </html>
    <?php
}
else
{
    //echo".$ext File uploaded";
    ?>
    <html>
        <script>
            alert("File uploaded");

        </script>
    </html>
    <?php
    $im=new Imagick($ftmp);
    $im->setImageFormat('jpg');
    file_put_contents("/var/www/html/Image/Converted/a.jpg",$im);//Path where converted image will be saved on localhost.
    ?>
    <html>
        <script>
            function ok()
            {
                location.href='/Image/1.html';
            }
        </script>
        <img src="/Image/Converted/a.jpg" height="900" width="600">
        <input type=Button Value=Done onclick="ok()">
    </html>
    <?php
    $im->clear();
    $im->destroy();

}?>

HTML code. Insert your name of php file in the action tag.

<html>
<body>
    <h1 align=center>Upload image</h1>
    <form action=*.php method=POST enctype='multipart/form-data'>
        Please select the file:<br>
        <input type=file name=file><br>
        <input type=SUBMIT value=Submit>
        <input type=Reset value=Reset>
    </form>
</body>