Jump to content

Replace the original image with a resized variation of itself.


Recommended Posts

Posted

Hi.

I have over 3k pages with an image field that populated with quite large images.

What I need to do is to replace the original image in this field with a resized version of original image. 

What would be optimal way to do that?

Posted

As far as I understand it right what you want to achieve, I would loop through all pages with an API script, delete the old image(s) of each, put the new image(s) to the same field and save the page. I have a similar situation on a site where a lot of images get synced with external sources very often and the delete & save method works reliable.

Posted (edited)
On 8/10/2021 at 2:44 PM, Zeka said:

What I need to do is to replace the original image in this field with a resized version of original image. 

What would be optimal way to do that?

You can resize the original image like this:

    // max width for landscape or max height for portrait
    $maxSize = 900;


    // then loop through images

        // get an image
        $image = $page->images->first();

        // is it portrait or landscape?
        $portrait = $image->height > $image->width;

        // set properties according to image orientation
        $property = $portrait ? 'height' : 'width';
        $width    = $portrait ? 0 : $maxSize;
        $height   = $portrait ? $maxSize : 0;

        // if the current size exceeds the max, proceed a resize
        if($image->$property > $maxSize) {
            $is = new ImageSizer($image->filename, ['quality' => 100]); // do not crucnch quality for original jpeg images !!
            $is->resize($width, $height);  // resizes the original image, Attention: also wipes out EXIF data, XMP etc. - only IPTC is kept!
        }

        // DONE!

    // end of loop

 

EDIT:

@Zeka If you have any further questions, maybe in regard of CLI script processing, please ask. 

https://github.com/rolandtoth/PageimageRemoveVariations/blob/master/PageimageRemoveVariationsConfig.php#L121-L159

 

 

Edited by horst
  • Like 1
Posted

Hi @horst

Thanks for the code. 

Just one more question, how to update image/width/ratio info in DB for resized image? 

 

  • Like 1
Posted

Hi @Zeka

I see, the DB data isn't updated by this method. Also when calling the ->width and ->height properties on image objects it returns the correct new values. But nevertheless we need to update the data in the DB too. (Also I don't know where or for what it is used ATM. ? )

I don't know any API method for updating this via page images or page files. The only thing I know is to alter it via the database object directly:

        $TABLE = "field_images";
        $WHERE = "data='{$image->name}' AND pages_id={$page->id}";
        $SET   = "filesize=" . filesize($image->filename) . ", width={$image->width}, height={$image->height}";
        $statement = "UPDATE {$TABLE} SET {$SET} WHERE {$WHERE};";

        $pdoStatement = $database->prepare($statement);
        $res = $database->exec($pdoStatement);

We need to update the filesize, width and height. Ratio is unchanged and can be kept. So depending on your route you already have the fieldname(s) and page id(s) stored in params. The above is a bare example that works but better should / can be used as a function. (?) 

  • Like 1
  • 2 months later...
Posted
On 8/16/2021 at 4:15 PM, horst said:

Hi @Zeka

I see, the DB data isn't updated by this method. Also when calling the ->width and ->height properties on image objects it returns the correct new values. But nevertheless we need to update the data in the DB too. (Also I don't know where or for what it is used ATM. ? )

I don't know any API method for updating this via page images or page files. The only thing I know is to alter it via the database object directly:

        $TABLE = "field_images";
        $WHERE = "data='{$image->name}' AND pages_id={$page->id}";
        $SET   = "filesize=" . filesize($image->filename) . ", width={$image->width}, height={$image->height}";
        $statement = "UPDATE {$TABLE} SET {$SET} WHERE {$WHERE};";

        $pdoStatement = $database->prepare($statement);
        $res = $database->exec($pdoStatement);

We need to update the filesize, width and height. Ratio is unchanged and can be kept. So depending on your route you already have the fieldname(s) and page id(s) stored in params. The above is a bare example that works but better should / can be used as a function. (?) 

Hi @horst Thanks for the code;

I have found that there is 'saveFileCols' method that can be used to set values of these properties in DB. 

$fieldtype = $fields->get('image')->getFieldtype();
$image = $page->get('image');

$fieldtype->saveFileCols($page, $fields->get('image'), $image, [
  'width' => $image->width,
  'height' => $image->height,
  'ratio' => $image->ratio,
  'filesize' => $image->filesize
]);

 

  • Like 2
  • Thanks 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...