Skip to content

Instantly share code, notes, and snippets.

@finagin
Last active July 4, 2017 09:23
Show Gist options
  • Save finagin/a14cef1b31fbf556d91b5b2f4b2d680e to your computer and use it in GitHub Desktop.
Save finagin/a14cef1b31fbf556d91b5b2f4b2d680e to your computer and use it in GitHub Desktop.
<?php
function panorama($file_name, $water_mark = false, $r = 255, $g = 255, $b = 255)
{
list($width_x, $size) = getimagesize($file_name);
if ($width_x < $size) {
throw new Exception('The panorama must be horizontal');
}
if (($src_im = imagecreatefromjpeg($file_name)) === false) {
if (($src_im = imagecreatefrompng($file_name)) === false) {
if (($src_im = imagecreatefromgif($file_name)) === false) {
throw new Exception('Undefined format');
}
}
}
var_dump($src_im);
$sections = intval(ceil($width_x / $size));
$diff = ($sections * $size - $width_x) / 2;
for ($i = 0; $i < $sections; $i++) {
$dst_im = imagecreatetruecolor($size, $size);
$bg_color = imagecolorallocate($dst_im, $r, $g, $b);
imagefilledrectangle($dst_im, 0, 0, $size, $size, $bg_color);
$dst_x = $diff * ( ! boolval($i));
$dst_y = 0;
$src_x = ($size * $i) - $diff * (boolval($i));
$src_y = 0;
$src_w = $size - (( ! boolval($i) || $i + 1 == $sections) ? $diff : 0);
$src_h = $size;
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
if ($water_mark && $i + 1 == $sections) {
$water_mark = imagecreatefrompng($water_mark);
list($water_mark_x, $water_mark_y) = getimagesize($water_mark);
$k1 = $size / 2 / $water_mark_y;
$w_x = ($k1 * $water_mark_x);
$w_y = ($k1 * $water_mark_y);
$dst_x = $size - $w_x + 1;
$dst_y = $size - $w_y + 1;
$src_x = 0;
$src_y = 0;
$dst_w = $w_x;
$dst_h = $w_y;
$src_w = $water_mark_x;
$src_h = $water_mark_y;
imagecopyresized($dst_im, $water_mark, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
}
header('Content-Type: image/png');
imagepng($dst_im, 'panorama_'.($i + 1).'.png', 0);
imagedestroy($dst_im);
}
}
// panorama('IMG_9115.JPG');
// panorama('IMG_9115.PNG');
// panorama('IMG_9115.GIF');
// output ./panorama_(\d+).png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment