Jump to content

Croppable Image 3


horst

Recommended Posts

This is the version to stay. It is close to get stable 1.0.0 ! :)

The quirks with the first tries in PW3 was mainly because of misunderstandings between Ryan and me when first time talking about the changes in core image field. And also because my insuficient knowledge of PW ;)

But know we have a solution that (only) extends the core image field with additional functionality. Possible changes, enhancements on the core imagefield will automatically inherited know, like it was with the versions before the core image change.

  • Like 4
Link to comment
Share on other sites

15 hours ago, horst said:

and its now approved and available in the modules directory: http://modules.processwire.com/modules/croppable-image3/ :)

Come on, @horst! You could be a modest person, but publishing modules under different names is too much. I am used to rely on authority a bit while choosing a module to download. Your name and the vast amount of good stuff made under it has always assured me I can trust that another one new. Probable same for others. Or is some technical issue?

  • Like 1
Link to comment
Share on other sites

Simply overseen this. I think, when adding a module to the directory, most inputfields get prepopulated with the github-data, where my name is "horst-n", because "horst" wasn't available.

But I could change the module authors name to "horst". :) Thanks for pointing me to it!

Link to comment
Share on other sites

Feature request: Would it be possible to define an aspect ration (16:9, 4:3, …) instead of using px values? I have a case where I want to output different resolutions for retina/desktop/mobile/… and want to avoid a loss of resolution due to cropping (putting high px doesn't make sense either because not always the client has high resolution images at hand…)

  • Like 2
Link to comment
Share on other sites

It isn't designed to support this. But I also don't see much disadvantage for this usecase. Lets assume the width you want to support is 1400, 960, 480.

  • Setup the image field with min-width: 1400 and min-height: 788  (= 1400 / 16 * 9)
  • Create a cropsetting image16x9,
  • call it with 100% quality as master-source for the variations
$master = $page->images->first()->getCrop("image16x9", "quality=100");
$large = $master->width(1400);
$medium = $master->width(960);
$small = $master->width(480);

Or directly use the markup srcset module from @tpr and only pass the master to it!

Edited by horst
added link to markup srcset
  • Like 2
Link to comment
Share on other sites

hi horst,

just wanted to mention that  if you change an existing imagefield to croppableimage3 it will NOT show the crop settings on the input tab. after saving the field the textarea showed up correctly. maybe that could happen automatically?

thanks for one of those essential modules finally on pw3!

Link to comment
Share on other sites

sorry horst, i was unclear! of course i changed the type to croppableimage3 and SAVED it. then i looked around and around and clicked tab after tab... it was set to croppableimage3 but the textarea for the settings was not there! i cleared the cache, still not there. then i saved the field AGAIN and it finally appeared ;)

not a big deal but still worth to improve i think (if i'm not the only one experiencing this for whatever reason...)

Link to comment
Share on other sites

Enclosed you find a json file with my attempt of a GERMAN translation. Feedback is welcome (please as PM so we do not loose focus on the main topic of this thread.)

If everything's fine, I will pass it to the German language team for inclusion with the forth-coming PW V3 strings.

site--modules--croppableimage3--lang--croppableimage3textcollection-php.json

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Anyone successfully using CroppableImage3 in a repeater field?

I just converted a CropImage field (used in a repeater) over to CroppableImage3 and get an error every time I try to edit any page with the repeater on now (the front end is working fine.) It's possible this is a problem with the repeater code though.

Link to comment
Share on other sites

This is resolved now. It was a problem with the underlying repeater, not the CroppableImage3 field in it. I had earlier used the API to migrate a new field into the template used by the repeater but forgot to reflect the change in the field data (specifically, the repeaterFields settings on the field.) Now I have the repeater and its template in sync, all works well. 

  • Like 2
Link to comment
Share on other sites

As promised, here's the script with delete powers. Only tested on a small subset of an installation I'm switching over to CroppableImage3 so do your own testing please!

Spoiler

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CropImage &rArr; CroppableImage3 File Conversion</title>
    <style>
.missing     {text-decoration:line-through;}
.error       {background-color:#FDD;}
.ok          {background-color:#DFD;}
.take-action {background-color:#DDF;}
    </style>
  <head>
  <body>
    <h1>CropImage &rArr; CroppableImage3 Image File Migration</h1>
    <h2>Candidate Fields</h2>
<?php
// for PW3+
// (UPDATES by @jacmaes   bugfix for $new filename from below post!)
// (UPDATES by @netcarver debugIteration bugfix, PW3 compatibility, formatting tweaks, source file deletion switch)

$debugIteration = true;     // true to iterate only over the first page with a desired field, false to iterate over all pages
$doFilecopy     = false;    // true to really copy variation files, false for debug purposes

/** ========================= BE VERY SURE YOU WANT TO PROCEED IF SETTING THE FOLLOWING TO TRUE ========================
 * Set the following boolean to true if you want the original image variation files deleted.
 *
 * In order to prevent total loss of the images, deletion is only attempted if...
 * $doOldfileDeletion is true **AND** $doFileCopy is false **AND** the original file exists **AND** the replacement file exists
 * ========================== BE VERY SURE YOU WANT TO PROCEED IF SETTING THE FOLLOWING TO TRUE ========================*/
$doOldfileDeletion = false; // true to delete old file variations.


$oldFieldtype = 'CropImage';
$newFieldtype = 'CroppableImage3';
$timelimit = 120;  // per single page

// bootstrap PW
include(dirname(__FILE__) . '/index.php');


// collect fields and cropsetting names
$collection = array();
echo "<ul>";
foreach($fields as $f) {
    if($f->type != 'Fieldtype' . $oldFieldtype) continue;
    $collection[$f->name] = array();
    echo "<li>{$f->type} : {$f->name}</li>";
    $thumbSettings = preg_match_all('#(.*?),.*?\n#msi' , trim($f->thumbSetting) . "\n", $matches, PREG_PATTERN_ORDER);
    if(!$thumbSettings) continue;
    $collection[$f->name] = $matches[1];
    echo "<ul>";
    foreach($collection[$f->name] as $suffix) {
        echo "<li>{$suffix}</li>";
    }
    echo "</ul>";
}
echo "</ul>";
echo "<hr />";
// We also have to look at CroppableImage3 fields because, after you change your fields over, the above search won't
// find the assets that now need deleting...
echo "<ul>";
foreach($fields as $f) {
    if($f->type != 'Fieldtype' . $newFieldtype) continue;
    $collection[$f->name] = array();
    echo "<li>{$f->type} : {$f->name}</li>";
    $thumbSettings = preg_match_all('#(.*?),.*?\n#msi' , trim($f->cropSetting) . "\n", $matches, PREG_PATTERN_ORDER);
    if(!$thumbSettings) continue;
    $collection[$f->name] = $matches[1];
    echo "<ul>";
    foreach($collection[$f->name] as $suffix) {
        echo "<li>{$suffix}</li>";
    }
    echo "</ul>";
}
echo "</ul>";
echo "<hr />";

$pages_visited      = 0;
$images_visited     = 0;
$variations_visited = 0;
$variations_copied  = 0;
$variations_deleted = 0;

// now iterate over all pages and rename or copy the crop variations
echo "<h2>Asset Scan</h2>";
echo "<ul>";
foreach($pages->find("include=all") as $p) {
    set_time_limit($timelimit);  // reset the timelimit for this page
    foreach($collection as $fName => $suffixes) {
        if(!$p->$fName instanceof \ProcessWire\Pageimages) {
            continue;
        }
        $images = $p->$fName;
        if(0 == $images->count()) {
            continue;
        }
        echo "<li>Page \"<strong>{$p->title}</strong>\" <em>directory \"site/assets/files/{$p->id}/\"</em><ol>";
        foreach($images as $image) {
            echo "Image \"<strong>{$image->name}</strong>\"<ul>";
            $images_visited++;
            foreach($suffixes as $suffix) {
                $variations_visited++;
                $errors  = array();
                $dispold = "{$suffix}_" . $image->name;
              	$dispnew = str_replace(".", ".-{$suffix}.", $image->name);

                $dir = dirname($image->filename).'/';
                $old = $dir . $dispold;
                $new = $dir . $dispnew;

                $old_present = file_exists($old);
                $new_present = file_exists($new);

                /* if (!$old_present) $dispold = "<span class='missing error'>$dispold</strike> (missing)"; */
                /* if ( $new_present) $dispnew = "<span class='ok'>$dispnew</span> (present)"; */

                $old_classes = array();
                $new_classes = array();
                $msg         = '';
                if (!$old_present) $old_classes[] = 'missing';
                if (!$new_present) $new_classes[] = 'missing';

                if (!$old_present && !$new_present) {
                    // Both missing => Cannot migrate this variation, it needs to be regenerated from the original.
                    $old_classes[] = 'error';
                    $new_classes[] = 'error';
                    $msg           = '<span class="take-action">Recreate from source image</span>';
                } else if (!$old_present &&  $new_present) {
                    // Old not there, new is => All done! Migration complete
                    $msg           = '<span class="ok">Migrated</span>';
                } else if ( $old_present && !$new_present) {
                    // Old there, new not there => Ready to copy!
                    $new_classes[] = 'take-action';
                    if (!$doFilecopy) $msg = 'Ready to copy';
                } else {
                    // Both present, ready to delete old copy!
                    $old_classes[] = 'error';
                    if (!$doOldfileDeletion) $msg = 'Ready to delete original';
                }

                $old_classes = implode(' ', $old_classes);
                $new_classes = implode(' ', $new_classes);
                echo "<li><em>Variation: $suffix</em> &mdash; <span class='$old_classes'>$dispold</span> &rArr; <span class='$new_classes'>$dispnew</span> <strong>$msg</strong>";
                if($doFilecopy && $old_present && !$new_present) {
                    $res = @copy($old, $new);
                    if ($res) {
                        $variations_copied++;
                        $res = "<span class='ok'>Success</span>";
                    } else {
                        $res = "<span class='error'>FAILED</span>";
                    }
                    echo " <strong>Filecopy: $res</strong>";
                }

                if (!$doFilecopy && $doOldfileDeletion && $old_present && $new_present) {
                    $res = @unlink($old);
                    if ($res) {
                        $variations_deleted++;
                        $res = "<span class='ok'>Success</span>";
                    } else {
                        $res = "<span class='error'>FAILED</span>";
                    }
                    echo " <strong>Delete of original: $res</strong>";
                }

                echo "</li>";
            }
            echo "</ul>";
        }
        echo "</ol></li>";
        if($debugIteration) break;
    }
    $pages->uncache($p); // just in case we need the memory
    $pages_visited++;
    if($debugIteration) break;
}
echo "</ul><p>Visited $pages_visited pages.</p>";
echo "<p>Visited $images_visited images.</p>";
echo "<p>Visited $variations_visited variations.</p>";
echo "<p>Copied  $variations_copied variations.</p>";
echo "<p>Deleted $variations_deleted variations.</p>";
?>
  </body>
</html>

 

cropImage3-02.png

  • Like 5
Link to comment
Share on other sites

I finally took the plunge and installed pw 3.x... and of course CroppableImage3.... 
Honestly, CroppableImage was one of the reasons pw 3.x was a no-go for a long time.... 

I was just wondering, if the 'Crop' [from the original Image field] could be hidden somehow, since I don't want the user creating 5 different variations without purpose anyway... [or should this be asked in a different forum area?]

Excellent module nevertheless and always in my top3!

Link to comment
Share on other sites

18 minutes ago, videokid said:

Honestly, CroppableImage was one of the reasons pw 3.x was a no-go for a long time.... 

same here. but i went the risky way and it was released just in time :) i still think that this should be a core feature...

you could hide the button easily with some JS or CSS, for example using admin custom files or a little hook. do this once and you will be able to do many other little tweaks in the admin without asking for support ;)

  • Like 2
Link to comment
Share on other sites

12 minutes ago, bernhard said:

you could hide the button easily with some JS or CSS, for example using admin custom files or a little hook.

Your absolutely right! I'm already using Admin Custom Files.... I guess sometimes the obvious isn't obvious enough... :mellow:

Link to comment
Share on other sites

First of all: Great module! I just ran into one issue though, perhaps I`m doing something wrong here, but when i`m accept the image the cropped image is created and available in the assets, but in the not showing (or replacing) the image in the backend.

gif.gif

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...