Script php

Fermé
hamza_11 Messages postés 25 Date d'inscription jeudi 19 avril 2012 Statut Membre Dernière intervention 18 mai 2012 - Modifié par irongege le 10/05/2012 à 13:12
hamza_11 Messages postés 25 Date d'inscription jeudi 19 avril 2012 Statut Membre Dernière intervention 18 mai 2012 - 10 mai 2012 à 15:20
Bonjour,

je veut un script php qui peut écrire un texte sur mes image,svp
A voir également:

2 réponses

SmokeMax Messages postés 66 Date d'inscription mercredi 28 décembre 2011 Statut Membre Dernière intervention 23 août 2012 26
9 mai 2012 à 18:10
Tu veux un café et des biscuits aussi ? Si tu expliquais en détail et plus poliment que ça, ça aiderait
0
hamza_11 Messages postés 25 Date d'inscription jeudi 19 avril 2012 Statut Membre Dernière intervention 18 mai 2012
9 mai 2012 à 18:45
Re-Bonjour


s'il vous plait je veut crée un script php qui permet d'écrire un texte sur les images de mon site web comme par exemple "bonjour membre"
0
Templier Nocturne Messages postés 7734 Date d'inscription jeudi 22 janvier 2009 Statut Membre Dernière intervention 21 mai 2016 1 102
9 mai 2012 à 18:48
oui, et où est le problème ?...
0
hamza_11 Messages postés 25 Date d'inscription jeudi 19 avril 2012 Statut Membre Dernière intervention 18 mai 2012
9 mai 2012 à 18:52
le problème que j'ai plusieurs d'image je veut écrire une texte une seul fois
0
Templier Nocturne Messages postés 7734 Date d'inscription jeudi 22 janvier 2009 Statut Membre Dernière intervention 21 mai 2016 1 102
9 mai 2012 à 18:53
oui, et ?

où est le problème ?
0
hamza_11 Messages postés 25 Date d'inscription jeudi 19 avril 2012 Statut Membre Dernière intervention 18 mai 2012
9 mai 2012 à 18:53
j'ai trouver ce script mais j'arrive pas à le tourner


<?php

/**
* thumbs_builder
*
* @author Thomas RAMBAUD
* @copyright 2008
* @version 1.0
* @access public
*/
class thumbs_builder
{

protected $_source, $_thumbs_quality, $_save_directory, $_border, $_text, $_icon,
$_current_thumb, $_allowed_position;

public function __construct($source_src, $save_directory, $thumbs_quality = 100)
{
$this->set_source_src($source_src);
$this->set_save_directory($save_directory);
$this->set_thumbs_quality($thumbs_quality);
$this->_allowed_position = array('top', 'bottom', 'left', 'right', 'center');
}

public function get_source_path()
{
return $this->_source['src'];
}

public function get_source_width()
{
return $this->_source['sizes'][0];
}

public function get_source_height()
{
return $this->_source['sizes'][1];
}

public function get_save_directory()
{
return $this->_save_directory;
}

public function get_current_thumb()
{
return $this->_current_thumb['img'];
}

public function get_current_thumb_width()
{
return $this->_current_thumb['sizes'][0];
}

public function get_current_thumb_height()
{
return $this->_current_thumb['sizes'][1];
}

public static function get_random_string($chars_count = 4)
{
$caracteres = "123AaBbCcDdEe456fGgHhIiJjKkLlMmNn789PQqRrSsTtUuVvWwXxYyZz0";
$chaine = '';
srand(time());
for ($i = 0; $i < $chars_count; $i++)
$chaine .= substr($caracteres, rand() % (strlen($caracteres)), 1);
return $chaine;
}

public static function get_image_extension($fn_or_path)
{
return strtolower(substr($fn_or_path, strrpos($fn_or_path, '.') + 1));
}

public static function get_rgb_from_hexa($hex)
{
if ($hex[0] == '#')
$hex = substr($hex, 1);
$nb_cars = strlen($hex);
if ($nb_cars == 6)
list($r, $g, $b) = array($hex[0] . $hex[1], $hex[2] . $hex[3], $hex[4] . $hex[5]);
elseif ($nb_cars == 3)
list($r, $g, $b) = array($hex[0] . $hex[0], $hex[1] . $hex[1], $hex[2] . $hex[2]);
else
return false;
$r = hexdec($r);
$g = hexdec($g);
$b = hexdec($b);
return array($r, $g, $b);
}

public function set_source_src($source_src)
{
if (file_exists($source_src)) {
$this->_source['src'] = $source_src;
$this->_source['sizes'] = getimagesize($source_src);
$this->_source['img'] = $this->build_image_from_path($source_src, $this->_source['sizes'][2]);
return true;
} else {
return false;
}
}

public function set_save_directory($save_directory)
{
if (is_dir($save_directory)){
$this->_save_directory = $save_directory;
return true;
}
else{
return false;
}
}

public function set_thumbs_quality($quality)
{
$this->_thumbs_quality = ($quality >= 0 && $quality <= 100) ? $quality : 100;
}

public function set_icon($icon_src, $position_top, $position_left, $alpha = null)
{
if (file_exists($icon_src)) {
$this->_icon['src'] = $icon_src;
$this->_icon['sizes'] = getimagesize($icon_src);
$this->_icon['img'] = $this->build_image_from_path($icon_src, $this->_icon['sizes']);
$this->set_icon_position($position_top, $position_left);
if ($alpha != null)
$this->set_icon_alpha($alpha);
return true;
} else {
return false;
}
}

public function set_icon_alpha(int $alpha)
{
$this->_icon['alpha'] = $alpha;
}

public function set_icon_position($position_top, $position_left)
{
if (in_array($position_top, $this->_allowed_position) && in_array($position_left,
$allowed_positions)) {
$this->_icon['position_top'] = $position_top;
$this->_icon['position_left'] = $position_left;
return true;
} else {
return false;
}
}

public function set_text($text, $position_top, $position_left, $hex_color, $size = 20)
{
$this->_text['text'] = $text;
if (in_array($position_top, $this->_allowed_position) && in_array($position_left,
$allowed_positions)) {
$this->_text['position_top'] = $position_top;
$this->_text['position_left'] = $position_left;
$this->set_text_size($size);
$this->set_text_font(3);
$this->set_text_color($hex_color);
$this->reload_text_sizes();
return true;
} else {
return false;
}
}

public function set_text_color($hex)
{
$this->_text['color'] = self::get_rgb_from_hexa($hex);
}

public function set_text_font($font)
{
$this->_text['font'] = $font;
$this->reload_text_sizes();
}

public function set_text_size($size)
{
$this->_text['size'] = $size;
$this->reload_text_sizes();
}

public function remove_icon()
{
$this->_icon = null;
}

public function remove_text()
{
$this->_text = null;
}

public function resize($thumb_width, $thumb_height, $background_color, $filename = null)
{
$this->clear_current_thumb();

$this->_current_thumb['img'] = $this->create_image($thumb_width, $thumb_height);
$this->_current_thumb['sizes'][0] = $thumb_width;
$this->_current_thumb['sizes'][1] = $thumb_height;

$color = self::get_rgb_from_hexa($background_color);
$color = imagecolorallocate($this->_current_thumb['img'], $color[0], $color[1],
$color[2]);
imagefill($this->_current_thumb['img'], 0, 0, $color);

$ratio_source = $this->_source['sizes'][0] / $this->_source['sizes'][1];
$ratio_x = $this->_source['sizes'][0] / $thumb_width;
$ratio_y = $this->_source['sizes'][1] / $thumb_height;

$margin_left = 0;
$margin_top = 0;

if ($ratio_x > $ratio_y) {
$img_width = $thumb_width;
$img_height = $thumb_width / $ratio_source;
$margin_top = $thumb_height / 2 - $img_height / 2;
} elseif ($ratio_x < $ratio_y) {
$img_height = $thumb_height;
$img_width = $thumb_height * $ratio_source;
$margin_left = $thumb_width / 2 - $img_width / 2;
} else {
$img_width = $thumb_width;
$img_height = $thumb_height;
}

$thumb = $this->create_image($img_width, $img_height);
imagecopyresized($thumb, $this->_source['img'], 0, 0, 0, 0, $img_width, $img_height,
$this->_source['sizes'][0], $this->_source['sizes'][1]);
imagecopymerge($this->_current_thumb['img'], $thumb, $margin_left, $margin_top,
0, 0, $img_width, $img_height, 100);
return $filename !== null ? $this->save($filename) : true;
}

public function resize_crop($thumb_width, $thumb_height, $filename = null)
{
$this->clear_current_thumb();

$this->_current_thumb['img'] = $this->create_image($thumb_width, $thumb_height);

$this->_current_thumb['sizes'][0] = $thumb_width;
$this->_current_thumb['sizes'][1] = $thumb_height;

$ratio_x = $this->_source['sizes'][0] / $thumb_width;
$ratio_y = $this->_source['sizes'][1] / $thumb_height;

$mid_height = $thumb_height / 2;
$mid_width = $thumb_width / 2;

if ($ratio_x > $ratio_y) {
$final_width = $this->_source['sizes'][0] / $ratio_y;
$int_width = ($final_width / 2) - $mid_width;
imagecopyresampled($this->_current_thumb['img'], $this->_source['img'], -$int_width,
0, 0, 0, $final_width, $thumb_height, $this->_source['sizes'][0], $this->
_source['sizes'][1]);
} elseif ($ratio_x < $ratio_y) {
$final_height = $this->_source['sizes'][1] / $ratio_x;
$int_height = ($final_height / 2) - $mid_height;
imagecopyresampled($this->_current_thumb['img'], $this->_source['img'], 0, -$int_height,
0, 0, $thumb_width, $final_height, $this->_source['sizes'][0], $this->_source['sizes'][1]);
} else {
imagecopyresampled($this->_current_thumb['img'], $this->_source['img'], 0, 0, 0,
0, $thumb_width, $thumb_height, $this->_source['sizes'][0], $this->_source['sizes'][1]);
}

return $filename !== null ? $this->save($filename) : true;
}

public function resize_width($thumb_width, $filename = null)
{
$this->clear_current_thumb();
return $this->resize_side($thumb_width, 0, $filename);
}

public function resize_height($thumb_height, $filename = null)
{
$this->clear_current_thumb();
return $this->resize_side($thumb_height, 1, $filename);
}

private function resize_side($size, $side = 0, $filename = null)
{
$this->clear_current_thumb();

$other_side = $side == 0 ? 1 : 0;
$ratio = $this->_source['sizes'][$side] / $size;
$other_size = $this->_source['sizes'][$other_side] / $ratio;

$width = $side == 0 ? $size : $other_size;
$height = $side == 1 ? $size : $other_size;

$this->_current_thumb['img'] = $this->create_image($width, $height);
$this->_current_thumb['sizes'] = array(0 => $width, 1 => $height);

imagecopyresampled($this->_current_thumb['img'], $this->_source['img'], 0, 0, 0,
0, $width, $height, $this->_source['sizes'][0], $this->_source['sizes'][1]);

return (int)($side == 1 ? $width : $height);
}

function save($filename)
{
$this->put_icon_if_needed();
$this->put_text_if_needed();

return $this->save_thumb_image($this->_current_thumb['img'], $this->
_save_directory . $filename, $this->_thumbs_quality);
}

public function clean()
{
$this->destroy_image($this->_source['img']);
$this->destroy_image($this->_current_thumb['img']);
$this->destroy_image($this->_icon['img']);
$this->destroy_image($this->_text['img']);
}

private function create_image($width, $height)
{
$img = imagecreatetruecolor($width, $height);
$this->manage_transparency($img);
return $img;
}

private function destroy_image($img)
{
if ($img !== null) {
@imagedestroy($img);
unset($img);
}
}

private function clear_current_thumb()
{
$this->destroy_image($this->_current_thumb);
}

private function manage_transparency(&$toimage)
{
$type = $this->_source['sizes'][2];
if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_PNG) {
$index = imagecolortransparent($this->_source['img']);
if ($index >= 0) {
$color = imagecolorsforindex($fromimage, $index);
$index = imagecolorallocate($toimage, $color['red'], $color['green'], $color['blue']);
imagefill($toimage, 0, 0, $index);
imagecolortransparent($toimage, $index);
} elseif ($type == IMAGETYPE_PNG) {
$this->transparent_background($toimage);
}
}
}

private function transparent_background(&$img)
{
imagealphablending($img, false);
$black = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $black);
imagecolortransparent($img, $black);
}

private function build_image_from_path($path, $type)
{
switch ($type) {
case IMAGETYPE_GIF:
return imagecreatefromgif($path);
break;
case IMAGETYPE_PNG:
return imagecreatefrompng($path);
break;
case IMAGETYPE_JPEG:
default:
return imagecreatefromjpeg($path);
break;
}
}

private function save_thumb_image($thumb, $path, $quality)
{
switch (self::get_image_extension($path)) {
case 'gif':
return imagegif($thumb, $path);
break;
case 'png':
return imagepng($thumb, $path);
break;
case 'jpeg':
case 'jpg':
case 'jfif':
case 'jpe':
default:
return imagejpeg($thumb, $path, $quality);
break;
}
}

private function put_icon_if_needed()
{
if ($this->_icon !== null) {
$left = 0;
$top = 0;
$icon_width = $this->_icon['sizes'][0];
$icon_height = $this->_icon['sizes'][1];
$right = $this->_current_thumb['sizes'][0] - $icon_width;
$bottom = $this->_current_thumb['sizes'][1] - $icon_height;
$center_left = $right / 2;
$center_top = $bottom / 2;

switch ($this->_icon['position_left']) {
case 'left':
$this->_icon['margin_left'] = $left;
break;
case 'right':
$this->_icon['margin_left'] = $right;
break;
case 'center':
$this->_icon['margin_left'] = $center_left;
break;
default:
$this->_icon['margin_left'] = 0;
break;
}

switch ($this->_icon['position_top']) {
case 'top':
$this->_icon['margin_top'] = $top;
break;
case 'bottom':
$this->_icon['margin_top'] = $bottom;
break;
case 'center':
$this->_icon['margin_top'] = $center_top;
break;
default:
$this->_icon['margin_top'] = 0;
break;
}

if (!isset($this->_icon['is_built']) || !$this->_icon['is_built']) {
$this->transparent_background($this->_icon['img']);
$this->_icon['is_built'] = true;
}

imagecopymerge($this->_current_thumb['img'], $this->_icon['img'], $this->_icon['margin_left'],
$this->_icon['margin_top'], 0, 0, $icon_width, $icon_height, $this->_icon['alpha']);
}
}

private function put_text_if_needed()
{
if ($this->_text !== null) {
if (!isset($this->_text['color']) || $this->_text['color'] == null)
$this->_text['color'] = array(255, 255, 0);

$left = 0;
$top = 0;
$right = $this->_current_thumb['sizes'][0] - $this->_text['sizes'][0];
$bottom = $this->_current_thumb['sizes'][1] - $this->_text['sizes'][1];
$center_left = $right / 2;
$center_top = $bottom / 2;

switch ($this->_text['position_left']) {
case 'left':
$this->_text['margin_left'] = $left;
break;
case 'right':
$this->_text['margin_left'] = $right;
break;
case 'center':
$this->_text['margin_left'] = $center_left;
break;
default:
$this->_text['margin_left'] = 0;
break;
}

switch ($this->_text['position_top']) {
case 'top':
$this->_text['margin_top'] = $top;
break;
case 'bottom':
$this->_text['margin_top'] = $bottom;
break;
case 'center':
$this->_text['margin_top'] = $center_top;
break;
default:
$this->_text['margin_top'] = 0;
break;
}

$color = imagecolorallocate($this->_current_thumb['img'], $this->_text['color'][0],
$this->_text['color'][1], $this->_text['color'][2]);
$shadow_grey = imagecolorallocate($this->_current_thumb['img'], 128, 128, 128);

if (is_int(intval($this->_text['font']))) {
if (isset($this->_text['shadow']) && $this->_text['shadow'] == true)
imagestring($this->_current_thumb['img'], $this->_text['font'], $this->_text['margin_left'] +
1, $this->_text['margin_top'] + 1, $this->_text['text'], $shadow_grey);
imagestring($this->_current_thumb['img'], $this->_text['font'], $this->_text['margin_left'],
$this->_text['margin_top'], $this->_text['text'], $color);
} else {
if (isset($this->_text['shadow']) && $this->_text['shadow'] == true)
imagettftext($this->_current_thumb['img'], $this->_text['size'], 0, $this->
_text['margin_left'] + 1, $this->_text['margin_top'] + 1, $shadow_grey, $font, $this->
_text['text']);
imagettftext($this->_current_thumb['img'], $this->_text['size'], 0, $this->
_text['margin_left'], $this->_text['margin_top'], $color, $this->_text['font'],
$this->_text['text']);
}
}
}

private function reload_text_sizes()
{
$this->_text['sizes'][0] = imagefontwidth($this->_text['size']) * strlen($this->_text['text']);
$this->_text['sizes'][1] = imagefontheight($this->_text['size']) * 1;
}

}
0
Templier Nocturne Messages postés 7734 Date d'inscription jeudi 22 janvier 2009 Statut Membre Dernière intervention 21 mai 2016 1 102
9 mai 2012 à 18:57
pourquoi une telle usine à gaz juste pour ce que tu veux faire ?...

as tu cherché à travailler par toi même avant de prendre un script tout fait ?
0