Solution for 'Something went wrong with image creation. '

Although imagemagick was supported by my provider (Strato), and path seemed OK, I was still getting this error. Strato could not help me.

What I did was the following:

1 Installed http://www.zubrag.com/scripts/thumbnail-generator.php
in the root of my site ( 2 files). This script enables resizing and saving resulting image. This script assumes PHP GD is installed.

2) Adapted thumb.php to use Omeka image paths:

$images_folder = '/var/tmp/';

// Folder to save thumbnails, full path from the root folder, MUST end with slash.

$thumbs_folder = '/home/strato/www/yo/www.yoursite.nl/htdocs/OMEKA/archive/';

(use absolute paths)

3 Did some adaptions in
...Libraries/Omeka/File/Derivative/Image.php

a)
public static function createAll($originalFilePath, $fileMimeType)

self::checkOmekaCanMakeDerivativeImages();
return self::isDerivable($originalFilePath, $fileMimeType)
? self::createMyDerivativeImages($originalFilePath)
: false;

b)

/**
     * Generate all the derivative images for this file.
     *
     * Currently, derivative images include:
     * - 'fullsize'
     * - 'thumbnail'
     * - 'square_thumbnail'
     * New sizes could be added if a plugin were to
     * hook into the 'after_file_upload' hook, so this method does not need to
     * be extensible.
     *
     * @param string $path File to create derivatives of.
     * @return string Derived image name.
     */
    public static function createMyDerivativeImages($path)
    {

       $newFileName = self::_getFileName($path);

       $url="http://www.yoursite.nl/thumb.php?src=" . $newFileName ."&dest=square_thumbnails/". $newFileName ."&x=100&y=100"; 

        file_get_contents($url);

       $url="http://www.yoursite.nl/thumb.php?src=" . $newFileName ."&dest=thumbnails/". $newFileName ."&x=100&y=100"; 

        file_get_contents($url);

 $command= "cp ". $path . " /home/strato/www/yo/www.yoursite.nl/htdocs/OMEKA/archive/fullsize/".$newFileName;

        $imageName = "";

 exec($command, $result_array, $result_value);

        if ($result_value == 0) {
            return $imageName;
        } else {
            throw new Omeka_File_Derivative_Exception('Unix CP failed somehow');
        }

    }

An update to allow png to be uploaded en converted to jpg

in thumbs.php force jpg save

$image_type = 2;

In Image.php

/**
     * Generate all the derivative images for this file.
     *
     * Currently, derivative images include:
     * - 'fullsize'
     * - 'thumbnail'
     * - 'square_thumbnail'
     * New sizes could be added if a plugin were to
     * hook into the 'after_file_upload' hook, so this method does not need to
     * be extensible.
     *
     * @param string $path File to create derivatives of.
     * @return string Derived image name.
     */
    public static function createMyDerivativeImages($path)
    {

       $newFileName = self::_getFileName($path);

    $oldFileName = self::_getOldFileName($path);

       $url="http://www.berithsalom.nl/thumb.php?src=". $oldFileName ."&dest=square_thumbnails/". $newFileName ."&x=100&y=100"; 

        file_get_contents($url);

       $url="http://www.berithsalom.nl/thumb.php?src=" . $oldFileName ."&dest=thumbnails/". $newFileName ."&x=100&y=100"; 

        file_get_contents($url);

 $command= "cp ". $path . " /home/strato/www/be/www.berithsalom.nl/htdocs/OMEKA/archive/fullsize/".$newFileName;

      $imageName="";

 exec($command, $result_array, $result_value);

        if ($result_value == 0) {
            return $imagename;
        } else {
            throw new Omeka_File_Derivative_Exception('Unix CP failed someow');
        }

    }

  protected static function _getOldFileName($archiveFilename)
    {
        return basename($archiveFilename);
    }

@edgarh I just want to say thanks for this, and let others know about it.

I am using shared Linux hosting on 1and1, and even though they have ImageMagick installed, it doesn't work at all with Omeka. It simply wouldn't allow me to upload any image files, period. No JPG, no PNG, nothing.

I even tried compiling ImageMagick locally, but the local compile doesn't work either. I went through all of the other tricks (verify that tmp folder can be written to, has space, etc., and that ImageMagick can handle JPGs and such), but none of them did it.

So finally I remembered I had seen this in my previous searches and thought I would give it a try, and it worked!!! So again, thank you for this!

You're welcome. As this was a very quick and dirty solution, I made some changes afterwards because after a server-update any writing in /tmp was blocked. Also command() was inaccesible.

Hardcoded a larger thumbnail 200x200

thumbs.php

$images_folder = '/home/strato/www/yo/www.yoursite.nl/htdocs/OMEKA/archive/tmp/';

// after mkdir tmp myselves!

$thumbs_folder = '/home/strato/www/yo/www.yoursite.nl/htdocs/OMEKA/archive/';

In Image.php

public static function createMyDerivativeImages($path)
    {

       $newFileName = self::_getFileName($path);

    $oldFileName = self::_getOldFileName($path);

       $url="http://www.yoursite.nl/thumb.php?src=". $oldFileName ."&dest=square_thumbnails/". $newFileName ."&x=200&y=200"; 

        file_get_contents($url);

       $url="http://www.yoursite.nl/thumb.php?src=" . $oldFileName ."&dest=thumbnails/". $newFileName ."&x=200&y=200"; 

        file_get_contents($url);

      $imageName="";

$target="/home/strato/www/yo/www.yoursite.nl/htdocs/OMEKA/archive/fullsize/".$newFileName;

$result_value=copy ($path , $target);

        if ($result_value) {
            return $imagename;
        } else {
            throw new Omeka_File_Derivative_Exception('copy failed somehow');
        }
    }

  protected static function _getOldFileName($archiveFilename)
    {
        return basename($archiveFilename);
    }

I spoke too soon, but it's not your fault. The images now upload, but I don't see thumbnails at all.

I have spent the better part of the day tracking down the reason that this script wouldn't generate the thumbnails, and I think I finally figured it out. It seems 1and1 disables file_get_contents within PHP scripts, so the calls to thumb.php don't actually execute.

If you have any ideas for alternate URL calls, I am open to suggestions. I'll see if I can get curl working.

curl seems a good idea

OK, I managed to get this to work. I performed the copy first, since no image manipulation occurs there, then I did the curl calls to thumb.php. I also re-added the thumbnail constraint options, so that you can still configure them through the web interface.

public static function createMyDerivativeImages($path)
    {

        $newFileName = self::_getFileName($path);
        $command= "cp ". $path . " /path/to/archive/fullsize/".$newFileName;
        $imageName = "";

        exec($command, $result_array, $result_value);

        $fs_constraint = get_option('fullsize_constraint');
        $t_constraint = get_option('thumbnail_constraint');
        $s_constraint = get_option('square_thumbnail_constraint');

       $url="http://yoursite.com/thumb.php?src=" . $newFileName ."&dest=thumbnails/". $newFileName ."&x=".$t_constraint."&y=".$t_constraint;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);

        $url="http://yoursite.com/thumb.php?src=" . $newFileName ."&dest=square_thumbnails/". $newFileName ."&x=".$s_constraint."&y=".$s_constraint;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);

        if ($result_value == 0) {
            return $imageName;
        }
        else {
            throw new Omeka_File_Derivative_Exception('Unix CP failed somehow');
        }
    }

Incidentally, in thumb.php I am pointing $images_folder to the WEBROOT/archive/fullsize/ directory, since the script always returns good that way for me. I don't know why, and frankly I don't care at this point.

Since thumb.php works off of GD, I am curious if there is a way to adapt Omeka to use GD as a fallback in case ImageMagick just doesn't work for some reason.

Congrats! Good teamwork.

Lots of people seem to have trouble with imagemagick one way or the other...

Our addition, possibly sanitized, would help out.

Two steps forward, one step back.

Now I am getting "Out of memory" errors for the thumb.php script. It's probably because I am uploading lightly cropped 12 Mpx images, and I'm hitting some memory limit at the hosting provider level. 1and1, by the way, limits PHP to 60 MB for my plan (max for the next plan up is 80 MB), which sounds to me like it should be sufficient, but I'm not sure.

At this point, I have a number of choices:

1) Try to figure out why the Zubrag thumbnail script hits a memory limit and see if there is anything that can be done.

2) Try a different script, possibly written in ruby or perl.

3) Write my own script, possibly in ruby or perl.

4) Run a profiling script to look for images without thumbnails and run it completely outside the site through cron (again, possibly in conjunction with 2 or 3).

5) Go back and try to figure out exactly what is failing in 1and1's implementation of ImageMagick and/or Omeka's Image Derivative function.

Did I miss anything?

With luck, this is my last posting on this topic.

Since I am somewhat tenacious, I chose option 5 (from my previous post).

To get Omeka to output something useful in the error logs, I had to change the following code in the createImage function in application/libraries/Omeka/File/Derivative/Image.php:

exec($command.' 2>&1', $result_array, $result_value);
        if ($result_value == 0) {
            return $newFileName;
        } else {
            throw new Omeka_File_Derivative_Exception("Tried to run command: \n$command\n\nGot result: \n$result_array[0]\n$result_array[1]");
        }

In the error logs, I could see the output of the ImageMagick convert command for the first time:

2012-06-22T21:26:01-04:00 ERR (3): exception 'Omeka_File_Derivative_Exception' with message 'Tried to run command:
/path/to/convert '/tmp/cfee5f76ba0f296eb2f1853c8c5aaad5.jpg' -resize '800x800>' '/tmp/fullsize_cfee5f76ba0f296eb2f1853c8c5aaad5.jpg'
Got result:
libgomp: Thread creation failed: Resource temporarily unavailable' in /WEBROOT/application/libraries/Omeka/File/Derivative/Image.php:245

Turns out this error happens on 1and1 (and possibly other hosts as well). This error happens because of a memory issue with the convert utility. I found a solution here. The advice is to put the following line somewhere that the part of the program needing access to ImageMagick. Here's the line:

putenv("MAGICK_THREAD_LIMIT=1");

For now, I put it inside the createAll function in Image.php:

public static function createAll($originalFilePath, $fileMimeType)
    {
        // Don't try to make derivative images if we don't give a path to
        // ImageMagick.
        if (!get_option('path_to_convert')) {
            return false;
        }

        //Do this on 1and1

        putenv("MAGICK_THREAD_LIMIT=1");

        self::checkOmekaCanMakeDerivativeImages();
        return  self::isDerivable($originalFilePath, $fileMimeType)
                ? self::createDerivativeImages($originalFilePath)
                : false;
    }

This is probably not the best place to put it. Perhaps it should be elevated to something that can be configured in the config.ini file?

Anyway, if you are encountering a thumbnail generation problem, and you know ImageMagick is installed correctly, you can at least get the error output by modifying the exec command as above, just to see if there's output you can use to further troubleshoot.