Skip to content

Instantly share code, notes, and snippets.

@setola
Created May 5, 2012 18:56
Show Gist options
  • Save setola/2604756 to your computer and use it in GitHub Desktop.
Save setola/2604756 to your computer and use it in GitHub Desktop.
Slideshow: One big image for all
<?php
function merge_images($images, $config=null){
if(empty($images)) return 'No images';
$config = array_merge(
array(
'w' => '700',
'h' => '370',
'q' => '50',
'r' => false
),
$config
);
$combined_image = imagecreatetruecolor($config['w']*count($images), $config['h']);
$cache_name = '';
foreach($images as $image){
$cache_name .= $image['path'].';';
}
$cache_name .= serialize($config);
$cache_name = md5($cache_name);
$cache_dir = get_template_directory().'/cache/';
if (!@is_dir($cache_dir)){
if (!@mkdir($cache_dir)){
die('Couldn\'t create cache dir: '.$cache_dir);
}
}
$cache_url = get_bloginfo('template_url').'/cache/'.$cache_name.'.jpg';
$cache_path = $cache_dir.$cache_name.'.jpg';
if(!file_exists($cache_path) || IMAGE_MERGE_FORCE_REFRESH===true){
foreach($images as $array_index => $image){
$src = $image['url'].'?'.http_build_query($config, '', '&');
$info = getimagesize($src);
switch($info['mime']){
case 'image/jpeg':
$image = imagecreatefromjpeg($src);
break;
case 'image/png':
$image = imagecreatefrompng($src);
break;
case 'image/gif':
$image = imagecreatefromgif($src);
break;
default:
die('unknow mime type');
}
imagecopymerge(
$combined_image,
$image,
$array_index*$config['w'],
0, 0, 0,
$config['w'],
$config['h'],
100
);
imagejpeg(
$combined_image,
$cache_path,
$config['q']
);
}
}
return $cache_url;
}
<?php
$width = 1900;
$height = 500;
$config = array(
'w'=>$width,
'h'=>$height,
'q'=>'50'
);
define('IMAGE_MERGE_FORCE_REFRESH', false);
$images = wp_get_imagelist_for_slideshow(
'current',
array(
'default'=>'',
'field'=>'caption'
),
$config
);
$merged_image_uri = merge_images($images, $config);
//vd($images);
$tpl = '
<div
class="slideshow_image"
title="%title%"
style="background:url(\''.
$merged_image_uri.
'\') no-repeat scroll -%opos%px -%vpos%px transparent;width:'.
$width.
'px;height:'.
$height.
'px;%style%"
>
<div class="black_grad z_6"></div>
<div class="caption_container z_10">
<div class="container_16 z_10">
<div class="grid_16 text_right color_fff font_30 font_palatino">%caption%</div>
</div>
</div>
</div>
';
foreach ($images as $array_index => $image){
//printf($tpl, $array_index * $width, 0);
echo str_replace(
array(
'%caption%',
'%opos%',
'%vpos%',
'%title%',
'%style%'
),
array(
$image['caption'],
$array_index * $width,
'0',
$image['title'],
($array_index == 0) ? '' : 'display:none'
),
$tpl
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment