Skip to content

Instantly share code, notes, and snippets.

@yosun
Created October 4, 2023 04:55
Show Gist options
  • Save yosun/8b8ea3552c2ec6655e01cc3260f90bf0 to your computer and use it in GitHub Desktop.
Save yosun/8b8ea3552c2ec6655e01cc3260f90bf0 to your computer and use it in GitHub Desktop.
For example, if you have a ratio 1 image and upload it as 512x512 instead of 1024x1024 you get: https://replicate.com/p/463ayu3bhufo5twvla4byvpz3m instead of https://replicate.com/p/u2vrsv3buuvzifh3pautxcrdom
<?php
class SDXLNumerologyElement {
public $ratio;
public $width;
public $height;
}
class SDXLResizer {
public static $theNumerologyElements = array(
array('ratio' => 0.25, 'width' => 2048, 'height' => 512),
array('ratio' => 0.26, 'width' => 1984, 'height' => 512),
array('ratio' => 0.27, 'width' => 1920, 'height' => 512),
array('ratio' => 0.28, 'width' => 1856, 'height' => 512),
array('ratio' => 0.32, 'width' => 1792, 'height' => 576),
array('ratio' => 0.33, 'width' => 1728, 'height' => 576),
array('ratio' => 0.35, 'width' => 1664, 'height' => 576),
array('ratio' => 0.4, 'width' => 1600, 'height' => 640),
array('ratio' => 0.42, 'width' => 1536, 'height' => 640),
array('ratio' => 0.48, 'width' => 1472, 'height' => 704),
array('ratio' => 0.5, 'width' => 1408, 'height' => 704),
array('ratio' => 0.52, 'width' => 1344, 'height' => 704),
array('ratio' => 0.57, 'width' => 1344, 'height' => 768),
array('ratio' => 0.6, 'width' => 1280, 'height' => 768),
array('ratio' => 0.68, 'width' => 1216, 'height' => 832),
array('ratio' => 0.72, 'width' => 1152, 'height' => 832),
array('ratio' => 0.78, 'width' => 1152, 'height' => 896),
array('ratio' => 0.82, 'width' => 1088, 'height' => 896),
array('ratio' => 0.88, 'width' => 1088, 'height' => 960),
array('ratio' => 0.94, 'width' => 1024, 'height' => 960),
array('ratio' => 1.0, 'width' => 1024, 'height' => 1024),
array('ratio' => 1.07, 'width' => 960, 'height' => 1024),
array('ratio' => 1.13, 'width' => 960, 'height' => 1088),
array('ratio' => 1.21, 'width' => 896, 'height' => 1088),
array('ratio' => 1.29, 'width' => 896, 'height' => 1152),
array('ratio' => 1.38, 'width' => 832, 'height' => 1152),
array('ratio' => 1.46, 'width' => 832, 'height' => 1216),
array('ratio' => 1.67, 'width' => 768, 'height' => 1280),
array('ratio' => 1.75, 'width' => 768, 'height' => 1344),
array('ratio' => 2.0, 'width' => 704, 'height' => 1408),
array('ratio' => 2.09, 'width' => 704, 'height' => 1472),
array('ratio' => 2.4, 'width' => 640, 'height' => 1536),
array('ratio' => 2.5, 'width' => 640, 'height' => 1600),
array('ratio' => 2.89, 'width' => 576, 'height' => 1664),
array('ratio' => 3.0, 'width' => 576, 'height' => 1728),
array('ratio' => 3.11, 'width' => 576, 'height' => 1792),
array('ratio' => 3.62, 'width' => 512, 'height' => 1856),
array('ratio' => 3.75, 'width' => 512, 'height' => 1920),
array('ratio' => 3.88, 'width' => 512, 'height' => 1984),
array('ratio' => 4.0, 'width' => 512, 'height' => 2048)
);
public function getClosestElement($inputValue) {
$closestElement = null;
$minDifference = PHP_FLOAT_MAX;
foreach (self::$theNumerologyElements as $element) {
$difference = abs($element['ratio'] - $inputValue);
if ($difference < $minDifference) {
$minDifference = $difference;
$closestElement = $element;
}
}
return $closestElement;
}
public function resizeToSDXLPixels($imagePath) {
list($width, $height) = getimagesize($imagePath);
$ratio = $height / $width;
$closestElement = $this->getClosestElement($ratio);
$newWidth = $closestElement['width'];
$newHeight = $closestElement['height'];
$sourceImage = imagecreatefromjpeg($imagePath);
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($resizedImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
// Save or display the resized image as needed
// For example:
// imagejpeg($resizedImage, 'resized_image.jpg');
// or
// header('Content-Type: image/jpeg');
// imagejpeg($resizedImage);
imagedestroy($sourceImage);
imagedestroy($resizedImage);
}
}
// Example usage:
$resizer = new SDXLResizer();
$imagePath = 'path_to_your_image.jpg';
$resizer->resizeToSDXLPixels($imagePath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment