Skip to content

Instantly share code, notes, and snippets.

@Fursje
Created May 12, 2016 16:24
Show Gist options
  • Save Fursje/b37b4dec1b23c1b34fcf5590b769a1ab to your computer and use it in GitHub Desktop.
Save Fursje/b37b4dec1b23c1b34fcf5590b769a1ab to your computer and use it in GitHub Desktop.
Resize plex metadata images
<?php
$a = new img_resize();
$a->run();
class img_resize {
public $img_files = array();
public $img_path = "";
public $bad_files = array();
public $img_pix_limit = 921600; // 1280*720
public function __construct() {
$this->img_path = getcwd();
$this->img_path = '/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Metadata/';
}
public function run() {
$this->_get_images();
$this->_resize_images();
print_r($this->bad_files);
}
private function _resize_images() {
foreach ($this->img_files as $img_file) {
$mogrify_out = array();
$cmd_resize = sprintf("mogrify -resize 921600@ '%s'", $img_file);
exec($cmd_resize,$mogrify_out,$mogrify_ret);
if ($mogrify_ret == 0) {
print sprintf("info: successfully resized:[%s]\n",basename($img_file));
$this->img_convert_success[] = $img_file;
} else {
$this->img_convert_fail[] = $img_file;
}
}
}
private function _get_images() {
$cmd = sprintf("find '%s' -type f -size +400k",$this->img_path);
exec($cmd,$found,$ret);
if ($ret == 0) {
foreach ($found as $file) {
$identify_data = array();
$cmd_identify = sprintf("identify '%s'",$file);
exec($cmd_identify,$identify_data,$identify_ret);
if ($identify_ret == 0) {
// JPEG 1920x1080
if (preg_match("/.*JPEG\s([\d]{1,})x([\d]{1,})\s.*/",$identify_data[0],$identify_m)) {
$tmp_px_size = $identify_m[1]*$identify_m[2];
if ( $tmp_px_size > $this->img_pix_limit ) {
print sprintf("info: [%s] size:[%sx%s] Needs resizing: [%s > %s]\n",basename($file), $identify_m[1], $identify_m[2], $tmp_px_size, $this->img_pix_limit);
$this->img_files[] = $file;
}
} else {
$this->bad_files[] = $file;
}
} else {
$this->bad_files[] = $file;
}
}
}
}
}
@triksmelb
Copy link

Thank you so much for this, works great on Ubuntu via Terminal even on external SSD with Plex server data on it. Just modified the $this->img_path = accordingly and saved 10gig!

Was hoping to target 900gig Plex music library metadata in Artist and Albums now.

Would you mind sharing what would have to be modified in the script to get resize to max 5MB 500 x 500 pixels?

thank you in advance.

@Fursje
Copy link
Author

Fursje commented Dec 27, 2021

@triksmelb Haven't used this in a while, but my guess would be to change the value 921600 on line 11 & 28 to 250000 (500x500=250000). However, make sure to test this first on some test images you have. Goodluck! :)

@triksmelb
Copy link

thx @Fursje I'll try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment