Facebook Sliders Con Mootools and CSS – Ahora Con Generacion de Imagen en PHP
Posted on feb 18 in Generalby adminPrint

En el Blog de david walsh, me he encontrado con estos utiles Sliders echos con CSS y Usando Mootools, lo mejor de estos Sliders es que permiten redimencionar una imagen y guardar los cambios, bien el codigo usado en este proyecto es el siguiente:
HTML
HTML:
CSS
CSS:
-
#opacity-area, #width-area, #height-area { background:url(horizontal.jpg) 0 8px no-repeat; height:23px; width:500px; margin:0 0 5px 0; }
-
#opacity-slider, #width-slider, #height-slider { background:url(button-horizontal.jpg) no-repeat; width:33px; height:23px; cursor:pointer; }
Mootools
JavaScript:
-
window.addEvent('domready', function () {
-
/* opacity slider */
-
var mySlide = new Slider($('opacity-area'), $('opacity-slider'), {
-
steps: 100,
-
wheel: 1,
-
onChange: function(step){
-
$('opacity-label').set('html',step);
-
$('muse').set('opacity',step / 100);
-
}
-
}).set(100);
-
-
/* height slider */
-
var mySlide = new Slider($('height-area'), $('height-slider'), {
-
steps: 300,
-
wheel: 1,
-
onChange: function(step){
-
$('height-label').set('html',step);
-
$('muse').set('height',step);
-
}
-
}).set(300);
-
-
/* width slider */
-
var mySlide = new Slider($('width-area'), $('width-slider'), {
-
steps: 300,
-
wheel: 1,
-
onChange: function(step){
-
$('width-label').set('html',step);
-
$('muse').set('width',step);
-
}
-
}).set(300);
-
-
/* link click */
-
$('save-image').addEvent('click',function() {
-
alert('generating...');
-
$('muse').set('src','sliders-ajax-save-php.php?width=' + $('width-label').getText() + '&height=' + $('height-label').getText() + '&opacity=' + $('opacity-label').getText());
-
$('muse').setStyles({
-
'border':'2px dotted #999;',
-
'padding':'10px'
-
});
-
$('muse').set('opacity',100);
-
alert('done!');
-
});
-
});
PHP
PHP:
-
/* do i even do anything? */
-
{
-
//initial vars
-
$original_file = 'sliders/muse.png';
-
-
//create a new, virtual image
-
$image = imagecreatetruecolor($width, $height);
-
$source = imagecreatefrompng($original_file);
-
filter_opacity($source,$opacity);
-
imagecopyresized($image,$source,0,0,0,0,$width,$height,$original_width,$original_height);
-
-
$background = imagecolorallocate($image, 0, 0, 0);
-
imagecolortransparent($image, $background); // make the new temp image all transparent
-
imagealphablending($image, false); // turn off the alpha blending to keep the alpha channel
-
-
//output
-
imagepng($image);
-
imagedestroy($image);
-
}
-
-
-
/* from PHP.net -- http://us2.php.net/manual/en/function.imagefilter.php */
-
function filter_opacity( &$img, $opacity ) //params: image resource id, opacity in percentage (eg. 80)
-
{
-
//get image width and height
-
$w = imagesx( $img );
-
$h = imagesy( $img );
-
-
//turn alpha blending off
-
imagealphablending( $img, false );
-
-
//find the most opaque pixel in the image (the one with the smallest alpha value)
-
$minalpha = 127;
-
for( $x = 0; $x <$w; $x++ )
-
for( $y = 0; $y <$h; $y++ )
-
{
-
$alpha = ( imagecolorat( $img, $x, $y )>> 24 ) & 0xFF;
-
if( $alpha <$minalpha )
-
{ $minalpha = $alpha; }
-
}
-
-
//loop through image pixels and modify alpha for each
-
for( $x = 0; $x <$w; $x++ )
-
{
-
for( $y = 0; $y <$h; $y++ )
-
{
-
//get current alpha value (represents the TANSPARENCY!)
-
$colorxy = imagecolorat( $img, $x, $y );
-
$alpha = ( $colorxy>> 24 ) & 0xFF;
-
//calculate new alpha
-
if( $minalpha !== 127 )
-
{ $alpha = 127 + 127 * $opacity * ( $alpha - 127 ) / ( 127 - $minalpha ); }
-
else
-
{ $alpha += 127 * $opacity; }
-
//get the color index with new alpha
-
$alphacolorxy = imagecolorallocatealpha( $img, ( $colorxy>> 16 ) & 0xFF, ( $colorxy>> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
-
//set pixel with the new color + opacity
-
if( !imagesetpixel( $img, $x, $y, $alphacolorxy ) )
-
{ return false; }
-
}
-
}
-
return true;
-
}
Pueden ver un demo aqui.
Descarga el Codigo aqui.
O puedes visitar la web del autor aqui.
Nota: El codigo del autor tenia varios Bugs la version de mootols en La clase ELEMENT ya no implementa la funcion getText que usa el autor por lo cual se le tubo que agregar, a continuacion les dejo el codigo agregado:
JavaScript:
-
getText: function() {
-
return ($pick(this.innerText, this.textContent))
-
}
Related posts:
- Accessible News Slider: Plugin para realizar sliders con jQuery
- Breaking: MacBook Air.
- TemplatesFeed – Cientos de Plantillas gratis para tus paginas web
- Como crear un icono en varios colores con solo una imagen CSS


