Zeka Posted August 10, 2021 Share Posted August 10, 2021 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? Link to comment Share on other sites More sharing options...
rash Posted August 10, 2021 Share Posted August 10, 2021 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. Link to comment Share on other sites More sharing options...
horst Posted August 11, 2021 Share Posted August 11, 2021 (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 August 11, 2021 by horst 1 Link to comment Share on other sites More sharing options...
Zeka Posted August 16, 2021 Author Share Posted August 16, 2021 Hi @horst Thanks for the code. Just one more question, how to update image/width/ratio info in DB for resized image? 1 Link to comment Share on other sites More sharing options...
horst Posted August 16, 2021 Share Posted August 16, 2021 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. (?) 1 Link to comment Share on other sites More sharing options...
Zeka Posted November 6, 2021 Author Share Posted November 6, 2021 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 ]); 2 1 Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted November 7, 2021 Share Posted November 7, 2021 This thing looks like an ideal new admin action) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now