Skip to content

Instantly share code, notes, and snippets.

@LiamKarlMitchell
Created December 23, 2020 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiamKarlMitchell/abb2fc76af32f388a00da9bd1557d252 to your computer and use it in GitHub Desktop.
Save LiamKarlMitchell/abb2fc76af32f388a00da9bd1557d252 to your computer and use it in GitHub Desktop.
Check Magento 2 product images on disc.
#!/bin/bash
# Checks if images referenced on products in the DB exist on disc and checks that they are not smaller than 300x300 px.
# Runnable in bash.
# Requirements.
# apk add bash mysql-client imagemagick
# I also had a regexp to get only _1 _2 etc in the query but have removed it now.
# AND g.value REGEXP '_[0-9].(jpg|png)$'
# You can also do a where condition such as WHERE e.sku IN ('A', 'B')
mysql -h localhost -u yourdbuser -p'yourdbpass' yourdb --batch -s -S /var/run/mysqld/mysqld.sock <<\ENDSQL |
SELECT e.sku, g.value, g.value_id FROM catalog_product_entity_media_gallery g
JOIN catalog_product_entity_media_gallery_value v ON g.value_id = v.value_id
JOIN catalog_product_entity e ON e.entity_id = v.entity_id
ENDSQL
while read sku value value_id
do
# echo Checking for $sku $value $value_id
filename=pub/media/catalog/product$value
if [ -e "$filename" ];
then
identifyOutput=`identify -format "%w %h" "$filename"`
if [ $? -eq 1 ];
then
echo "$sku $filename Identify failed to run. ValueID: $value_id";
echo $identifyOutput
continue
fi
# Get the dimensions out.
read w h <<< "$identifyOutput"
# echo $filename Dimensions: $w $h
if (( w < 300 | h < 300 )); then
echo "$sku $filename is too small. $w x $h ValueID: $value_id";
fi
else
echo "$sku $filename does not exist on disk. ValueID: $value_id";
fi
done > checkimages.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment