-
I understand why generating conversions automatically when added to a model is a good idea, however, I have a scenario where it isn’t the preferred method. I am importing hundreds of thousands of images from a legacy app, and not only would it take forever to generate conversions for all of them, it would use up a lot more disk space. Since most of the images might never even be viewed again before being archived down the road, it only makes sense to generate conversions as needed. Is it possible to add two methods to the Media class, one to disable auto-generation when adding a media item, and one to generate a conversion manually? Something like:
The I really appreciate it. I am using v8 of the Media library. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
Hello, did you find any way to do it? I also occupy something like that |
Beta Was this translation helpful? Give feedback.
-
Did you ever get anywhere with this @danFWD ? I need to do the same thing.. but in my case I also need to generate the conversion on the fly with |
Beta Was this translation helpful? Give feedback.
-
I found a good tip on issue #1696. You can temporary empty the |
Beta Was this translation helpful? Give feedback.
-
Here is what I finally uncovered, though it still doesn't solve turning off the conversion when saving an image. /**
* This checks to make sure the thumb conversion exists,
* and if not, goes ahead and creates it.
*/
$fileManipulator = new \Spatie\MediaLibrary\Conversions\FileManipulator;
$media = $myModel->getMedia('images');
foreach( $media as $img ) {
if( !$img->hasGeneratedConversion('thumb') ) {
$fileManipulator->createDerivedFiles($img);
}
} Tested and this generates the conversion and saves the model conversion. |
Beta Was this translation helpful? Give feedback.
-
Actually... upon thinking about this a bit deeper, if I create a static variable on my model and use that to turn on and off the registering of a conversion, like so: class MyModel extends Model implements HasMedia {
use \Spatie\MediaLibrary\InteractsWithMedia;
public static $conversionsEnabled = true;
public function registerMediaConversions(Media $media = null): void {
if( self::$conversionsEnabled ) {
$this->addMediaConversion('thumb')
->width(420)
->height(265)
->quality(35)
->fit('crop', 420, 265)
->performOnCollections('images');
}
}
// etc
}
// my script
MyModel::$conversionsEnabled = false;
// import images
MyModel::$conversionsEnabled = true; // reset it |
Beta Was this translation helpful? Give feedback.
Here is what I finally uncovered, though it still doesn't solve turning off the conversion when saving an image.
Tested and this generates the conversion and saves the model conversion.