Skip to content

Instantly share code, notes, and snippets.

@symisc
Created March 30, 2021 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save symisc/ba6249f11ef52ce8b161215e1a14eea8 to your computer and use it in GitHub Desktop.
Save symisc/ba6249f11ef52ce8b161215e1a14eea8 to your computer and use it in GitHub Desktop.
Check if a given image is of the right dimension and if not try to resize it using the PixLab API - https://pixlab.io/cmd?id=smartresize
<?php
/*
* PixLab PHP Client which is just a single class PHP file without any dependency that you can get from Github
* https://github.com/symisc/pixlab-php
*/
require_once "pixlab.php";
# Check if a given image is of the right size: 800x600 and if not try to resize it.
# The command of interest here are header: https://pixlab.io/cmd?id=header & smartresize: https://pixlab.io/cmd?id=smartresize
$img = 'https://s-media-cache-ak0.pinimg.com/736x/60/aa/e4/60aae45858ab6ce9dc5b33cc2e69baf7.jpg';
$key = 'PIXLAB_API_KEY'; # Your PixLab API Key - Get yours from https://pixlab.io/dashboard
# Obtain image metadata at first via header
$pix = new Pixlab($key);
if( !$pix->get('header',[
"img" => $img
]) ){
echo $pix->get_error_message()."\n";
die;
}
$w = $pix->json->width;
$h = $pix->json->height;
if ($w > 800 || $h > 600){
echo "Resizing image from ${w}x${h} to near 800x600...\n";
# Invoke smart resize...
if( !$pix->get('smartresize',[
'img' => $img,
'width' => 800,
'height' => 600
]) ){
echo $pix->get_error_message()."\n";
}else{
echo "Resized image: ".$pix->json->link."\n";
}
}else{
echo "Uploaded image is of the correct size!\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment