예제 #1
0
function generate_transform_preview($ref){
	global $storagedir;	
        global $imagemagick_path;
	global $imversion;

	if (!isset($imversion)){
		$imversion = get_imagemagick_version();
	}

	$tmpdir = get_temp_dir();

        // get imagemagick path
        $command = get_utility_path("im-convert");
        if ($command==false) {exit("Could not find ImageMagick 'convert' utility.");}

        $orig_ext = sql_value("select file_extension value from resource where ref = '$ref'",'');
        $originalpath= get_resource_path($ref,true,'',false,$orig_ext);

	# Since this check is in get_temp_dir() omit: if(!is_dir($storagedir."/tmp")){mkdir($storagedir."/tmp",0777);}
	if(!is_dir(get_temp_dir() . "/transform_plugin")){mkdir(get_temp_dir() . "/transform_plugin",0777);}

       if ($imversion[0]<6 || ($imversion[0] == 6 &&  $imversion[1]<7) || ($imversion[0] == 6 && $imversion[1] == 7 && $imversion[2]<5)){
                $colorspace1 = " -colorspace sRGB ";
                $colorspace2 =  " -colorspace RGB ";
        } else {
                $colorspace1 = " -colorspace RGB ";
                $colorspace2 =  " -colorspace sRGB ";
        }

        $command .= " \"$originalpath\" +matte -delete 1--1 -flatten $colorspace1 -geometry 450 $colorspace2 \"$tmpdir/transform_plugin/pre_$ref.jpg\"";
        run_command($command);


	// while we're here, clean up any old files still hanging around
	$dp = opendir(get_temp_dir() . "/transform_plugin");
	while ($file = readdir($dp)) {
		if ($file <> '.' && $file <> '..'){
			if ((filemtime(get_temp_dir() . "/transform_plugin/$file")) < (strtotime('-2 days'))) {
				unlink(get_temp_dir() . "/transform_plugin/$file");
			}
		}
	}
	closedir($dp);

        return true;
  
}
function generate_transform_preview($ref)
{
    global $storagedir;
    global $imagemagick_path;
    global $imversion;
    if (!isset($imversion)) {
        $imversion = get_imagemagick_version();
    }
    $tmpdir = get_temp_dir();
    // get imagemagick path
    $command = get_utility_path("im-convert");
    if ($command == false) {
        exit("Could not find ImageMagick 'convert' utility.");
    }
    $orig_ext = sql_value("select file_extension value from resource where ref = '{$ref}'", '');
    $transformsourcepath = get_resource_path($ref, true, 'scr', false, 'jpg');
    //use screen size if available to save time
    if (!file_exists($transformsourcepath)) {
        $transformsourcepath = get_resource_path($ref, true, '', false, $orig_ext);
    }
    # Since this check is in get_temp_dir() omit: if(!is_dir($storagedir."/tmp")){mkdir($storagedir."/tmp",0777);}
    if (!is_dir(get_temp_dir() . "/transform_plugin")) {
        mkdir(get_temp_dir() . "/transform_plugin", 0777);
    }
    if ($imversion[0] < 6 || $imversion[0] == 6 && $imversion[1] < 7 || $imversion[0] == 6 && $imversion[1] == 7 && $imversion[2] < 5) {
        $colorspace1 = " -colorspace sRGB ";
        $colorspace2 = " -colorspace RGB ";
    } else {
        $colorspace1 = " -colorspace RGB ";
        $colorspace2 = " -colorspace sRGB ";
    }
    $command .= " \"{$transformsourcepath}\"[0] +matte -flatten {$colorspace1} -geometry 450 {$colorspace2} \"{$tmpdir}/transform_plugin/pre_{$ref}.jpg\"";
    run_command($command);
    // while we're here, clean up any old files still hanging around
    $dp = opendir(get_temp_dir() . "/transform_plugin");
    while ($file = readdir($dp)) {
        if ($file != '.' && $file != '..') {
            if (filemtime(get_temp_dir() . "/transform_plugin/{$file}") < strtotime('-2 days')) {
                unlink(get_temp_dir() . "/transform_plugin/{$file}");
            }
        }
    }
    closedir($dp);
    return true;
}
예제 #3
0
// are they requesting to change the original?
if (isset($_REQUEST['mode']) && strtolower($_REQUEST['mode']) == 'original') {
    $original = true;
} else {
    $original = false;
}
// if they can't download this resource, they shouldn't be doing this
// also, if they are trying to modify the original but don't have edit access
// they should never get these errors, because the links shouldn't show up if no perms
if ($access != 0 || $original && !$edit_access) {
    include "../../../include/header.php";
    echo "Permission denied.";
    include "../../../include/footer.php";
    exit;
}
$imversion = get_imagemagick_version();
// generate a preview image for the operation if it doesn't already exist
if (!file_exists(get_temp_dir() . "/transform_plugin/pre_{$ref}.jpg")) {
    //echo  "generating preview";
    //exit();
    generate_transform_preview($ref) or die("Error generating transform preview.");
}
# Locate imagemagick.
if (!isset($imagemagick_path)) {
    echo "Error: ImageMagick must be configured for crop functionality. Please contact your system administrator.";
    exit;
}
$command = get_utility_path("im-convert");
if ($command == false) {
    exit("Could not find ImageMagick 'convert' utility.");
}
function create_previews($ref, $thumbonly = false, $extension = "jpg", $previewonly = false, $previewbased = false, $alternative = -1, $ignoremaxsize = false, $ingested = false)
{
    global $keep_for_hpr, $imagemagick_path, $preview_generate_max_file_size, $autorotate_no_ingest;
    // keep_for_hpr will be set to true if necessary in preview_preprocessing.php to indicate that an intermediate jpg can serve as the hpr.
    // otherwise when the file extension is a jpg it's assumed no hpr is needed.
    # Debug
    debug("create_previews(ref={$ref},thumbonly={$thumbonly},extension={$extension},previewonly={$previewonly},previewbased={$previewbased},alternative={$alternative},ingested={$ingested})");
    if (!$previewonly) {
        // make sure the extension is the same as the original so checksums aren't done for previews
        $o_ext = sql_value("select file_extension value from resource where ref={$ref}", "");
        if ($extension == $o_ext) {
            debug("create_previews - generate checksum for {$ref}");
            generate_file_checksum($ref, $extension);
        }
    }
    # first reset preview tweaks to 0
    sql_query("update resource set preview_tweaks = '0|1' where ref = '{$ref}'");
    // for compatibility with transform plugin, remove any
    // transform previews for this resource when regenerating previews
    $tpdir = get_temp_dir() . "/transform_plugin";
    if (is_dir($tpdir) && file_exists("{$tpdir}/pre_{$ref}.jpg")) {
        unlink("{$tpdir}/pre_{$ref}.jpg");
    }
    # pages/tools/update_previews.php?previewbased=true
    # use previewbased to avoid touching original files (to preserve manually-uploaded preview images
    # when regenerating previews (i.e. for watermarks)
    if ($previewbased || $autorotate_no_ingest && !$ingested) {
        $file = get_resource_path($ref, true, "lpr", false, "jpg", -1, 1, false, "", $alternative);
        if (!file_exists($file)) {
            $file = get_resource_path($ref, true, "scr", false, "jpg", -1, 1, false, "", $alternative);
            if (!file_exists($file)) {
                $file = get_resource_path($ref, true, "pre", false, "jpg", -1, 1, false, "", $alternative);
                if (!file_exists($file) && $autorotate_no_ingest && !$ingested) {
                    $file = get_resource_path($ref, true, "", false, $extension, -1, 1, false, "", $alternative);
                }
            }
        }
    } else {
        if (!$previewonly) {
            $file = get_resource_path($ref, true, "", false, $extension, -1, 1, false, "", $alternative);
        } else {
            # We're generating based on a new preview (scr) image.
            $file = get_resource_path($ref, true, "tmp", false, "jpg");
        }
    }
    # Debug
    debug("File source is {$file}");
    # Make sure the file exists, if not update preview_attempts so that we don't keep trying to generate a preview
    if (!file_exists($file)) {
        sql_query("update resource set preview_attempts=ifnull(preview_attempts,0) + 1 where ref='{$ref}'");
        return false;
    }
    # If configured, make sure the file is within the size limit for preview generation
    if (isset($preview_generate_max_file_size) && !$ignoremaxsize) {
        $filesize = filesize_unlimited($file) / (1024 * 1024);
        # Get filesize in MB
        if ($filesize > $preview_generate_max_file_size) {
            return false;
        }
    }
    # Locate imagemagick.
    $convert_fullpath = get_utility_path("im-convert");
    if ($convert_fullpath == false) {
        debug("ERROR: Could not find ImageMagick 'convert' utility at location '{$imagemagick_path}'");
        return false;
    }
    # Handle alternative image file generation.
    global $image_alternatives;
    if (isset($image_alternatives) && $alternative == -1) {
        for ($n = 0; $n < count($image_alternatives); $n++) {
            $exts = explode(",", $image_alternatives[$n]["source_extensions"]);
            if (in_array($extension, $exts)) {
                # Remove any existing alternative file(s) with this name.
                $existing = sql_query("select ref from resource_alt_files where resource='{$ref}' and name='" . escape_check($image_alternatives[$n]["name"]) . "'");
                for ($m = 0; $m < count($existing); $m++) {
                    delete_alternative_file($ref, $existing[$m]["ref"]);
                }
                # Create the alternative file.
                $aref = add_alternative_file($ref, $image_alternatives[$n]["name"]);
                $apath = get_resource_path($ref, true, "", true, $image_alternatives[$n]["target_extension"], -1, 1, false, "", $aref);
                $source_profile = '';
                if ($image_alternatives[$n]["icc"] === true) {
                    $iccpath = get_resource_path($ref, true, '', false, $extension) . '.icc';
                    global $icc_extraction;
                    global $ffmpeg_supported_extensions;
                    if (!file_exists($iccpath) && $extension != "pdf" && !in_array($extension, $ffmpeg_supported_extensions)) {
                        // extracted profile doesn't exist. Try extracting.
                        extract_icc_profile($ref, $extension);
                    }
                    if (file_exists($iccpath)) {
                        $source_profile = ' -strip -profile ' . $iccpath;
                    }
                }
                # Process the image
                $version = get_imagemagick_version();
                if ($version[0] > 5 || $version[0] == 5 && $version[1] > 5 || $version[0] == 5 && $version[1] == 5 && $version[2] > 7) {
                    // Use the new imagemagick command syntax (file then parameters)
                    $command = $convert_fullpath . ' ' . escapeshellarg($file) . ($extension == 'psd' ? '[0] +matte' : '') . $source_profile . ' ' . $image_alternatives[$n]['params'] . ' ' . escapeshellarg($apath);
                } else {
                    // Use the old imagemagick command syntax (parameters then file)
                    $command = $convert_fullpath . $source_profile . " " . $image_alternatives[$n]["params"] . " " . escapeshellarg($file) . " " . escapeshellarg($apath);
                }
                $output = run_command($command);
                if (file_exists($apath)) {
                    # Update the database with the new file details.
                    $file_size = filesize_unlimited($apath);
                    sql_query("update resource_alt_files set file_name='" . escape_check($image_alternatives[$n]["filename"] . "." . $image_alternatives[$n]["target_extension"]) . "',file_extension='" . escape_check($image_alternatives[$n]["target_extension"]) . "',file_size='" . $file_size . "',creation_date=now() where ref='{$aref}'");
                }
            }
        }
    }
    if ($extension == "jpg" || $extension == "jpeg" || $extension == "png" || $extension == "gif") {
        if (isset($imagemagick_path)) {
            create_previews_using_im($ref, $thumbonly, $extension, $previewonly, $previewbased, $alternative, $ingested);
        } else {
            # ----------------------------------------
            # Use the GD library to perform the resize
            # ----------------------------------------
            # For resource $ref, (re)create the various preview sizes listed in the table preview_sizes
            # Only create previews where the target size IS LESS THAN OR EQUAL TO the source size.
            # Set thumbonly=true to (re)generate thumbnails only.
            $sizes = "";
            if ($thumbonly) {
                $sizes = " where id='thm' or id='col'";
            }
            if ($previewonly) {
                $sizes = " where id='thm' or id='col' or id='pre' or id='scr'";
            }
            # fetch source image size, if we fail, exit this function (file not an image, or file not a valid jpg/png/gif).
            if ((list($sw, $sh) = @getimagesize($file)) === false) {
                return false;
            }
            $ps = sql_query("select * from preview_size {$sizes}");
            for ($n = 0; $n < count($ps); $n++) {
                # fetch target width and height
                $tw = $ps[$n]["width"];
                $th = $ps[$n]["height"];
                $id = $ps[$n]["id"];
                # Find the target path
                $path = get_resource_path($ref, true, $ps[$n]["id"], false, "jpg", -1, 1, false, "", $alternative);
                if (file_exists($path) && !$previewbased) {
                    unlink($path);
                }
                # Also try the watermarked version.
                $wpath = get_resource_path($ref, true, $ps[$n]["id"], false, "jpg", -1, 1, true, "", $alternative);
                if (file_exists($wpath)) {
                    unlink($wpath);
                }
                # only create previews where the target size IS LESS THAN OR EQUAL TO the source size.
                # or when producing a small thumbnail (to make sure we have that as a minimum)
                if ($sw > $tw || $sh > $th || $id == "thm" || $id == "col") {
                    # Calculate width and height.
                    if ($sw > $sh) {
                        $ratio = $tw / $sw;
                    } else {
                        $ratio = $th / $sh;
                    }
                    # Portrait
                    $tw = floor($sw * $ratio);
                    $th = floor($sh * $ratio);
                    # ----------------------------------------
                    # Use the GD library to perform the resize
                    # ----------------------------------------
                    $target = imagecreatetruecolor($tw, $th);
                    if ($extension == "png") {
                        $source = @imagecreatefrompng($file);
                        if ($source === false) {
                            return false;
                        }
                    } elseif ($extension == "gif") {
                        $source = @imagecreatefromgif($file);
                        if ($source === false) {
                            return false;
                        }
                    } else {
                        $source = @imagecreatefromjpeg($file);
                        if ($source === false) {
                            return false;
                        }
                    }
                    imagecopyresampled($target, $source, 0, 0, 0, 0, $tw, $th, $sw, $sh);
                    imagejpeg($target, $path, 90);
                    if ($ps[$n]["id"] == "thm") {
                        extract_mean_colour($target, $ref);
                    }
                    imagedestroy($target);
                } elseif ($id == "pre" || $id == "thm" || $id == "col") {
                    # If the source is smaller than the pre/thm/col, we still need these sizes; just copy the file
                    copy($file, get_resource_path($ref, true, $id, false, $extension, -1, 1, false, "", $alternative));
                    if ($id == "thm") {
                        sql_query("update resource set thumb_width='{$sw}',thumb_height='{$sh}' where ref='{$ref}'");
                    }
                }
            }
            # flag database so a thumbnail appears on the site
            if ($alternative == -1) {
                sql_query("update resource set has_image=1,preview_extension='jpg',preview_attempts=0,file_modified=now() where ref='{$ref}'");
            }
        }
    } else {
        # If using ImageMagick, call preview_preprocessing.php which makes use of ImageMagick and other tools
        # to attempt to extract a preview.
        global $no_preview_extensions;
        if (isset($imagemagick_path) && !in_array(strtolower($extension), $no_preview_extensions)) {
            include dirname(__FILE__) . "/preview_preprocessing.php";
        }
    }
    return true;
}