7

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?

David
  • 4,717
  • 8
  • 35
  • 46

4 Answers4

10

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();
Jeroen
  • 13,056
  • 4
  • 42
  • 63
  • Thanks for the code. However, `destroy()` is deprecated in favor of `clear()`. https://www.php.net/manual/en/imagick.destroy.php – vee Jan 26 '21 at 14:27
2

Even easier to

sudo apt-get install ufraw ufraw-batch

then either use UFRaw (see man pages) or use ImageMagick which uses UFRaw

convert mypic.RAF mypic.jpeg
Alexey Vazhnov
  • 1,291
  • 17
  • 20
Keir
  • 557
  • 1
  • 6
  • 17
  • `convert` will discard your metadata (info like time taken and camera model), which may not be what you want. If anyone has an alternative that preserves your metadata, please do add it here. – Pieter Aug 07 '20 at 15:18
1

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;"/>
Bonzo
  • 5,169
  • 1
  • 19
  • 27
  • The updated version of dcraw that I am using doesn't support the -O output file file anymore. I found the the following replacement delegate works: `` That was for my unix based path, I imagine you could do the similar for windows by replacing `/usr/bin/dcraw` with `dcraw.exe` – ctrlplusb Jun 26 '15 at 10:18
-1

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>