source: extensions/gvideo/admin/thumbnails.php @ 7291

Last change on this file since 7291 was 7291, checked in by plg, 14 years ago

feature 1942 added: support PNG files for creating thumbnails

GD version 2 becomes mandatory

File size: 4.9 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5function get_image_with_band($srcImage, $gd_ver)
6{
7    $srcWidth = imagesx($srcImage);
8    $srcHeight = imagesy($srcImage);
9    $const = intval($srcWidth * 0.04);
10
11    if ($gd_ver >= 2)
12    {
13      $imgBand = imagecreatetruecolor($srcWidth + 6 * $const, $srcHeight);
14    }
15    else
16    {
17      $imgBand = imagecreate($srcWidth + 6 * $const, $srcHeight);
18    }
19
20    $white = imagecolorallocate($imgBand, 255, 255, 255);
21    $y_start = intval($srcHeight / 2);
22    $aug = intval($y_start / 4) + 1;
23    $i = 0;
24
25    while ($y_start + $i * $aug < $srcHeight - $const)
26    {
27      imagefilledrectangle($imgBand, $const, $y_start + $i * $aug - ($const / 2), 2 * $const - 1, $y_start + $i * $aug + ($const / 2) - 1, $white);
28      imagefilledrectangle($imgBand, $const, $y_start - $i * $aug - ($const / 2), 2 * $const - 1, $y_start - $i * $aug + ($const / 2) - 1, $white);
29
30      imagefilledrectangle($imgBand, $srcWidth + (4 * $const), $y_start + $i * $aug - ($const / 2), $srcWidth + (5 * $const) - 1, $y_start + $i * $aug + ($const / 2) - 1, $white);
31      imagefilledrectangle($imgBand, $srcWidth + (4 * $const), $y_start - $i * $aug - ($const / 2), $srcWidth + (5 * $const) - 1, $y_start - $i * $aug + ($const / 2) - 1, $white);
32
33      ++$i;
34    }
35
36    imagecopy($imgBand, $srcImage, 3 * $const, 0, 0, 0, $srcWidth, $srcHeight);
37    return $imgBand;
38}
39
40function Py_RatioResizeImg($url, $path, $newWidth, $newHeight)
41{
42    global $conf, $lang, $page;
43
44    if (!function_exists('gd_info'))
45    {
46      return false;
47    }
48
49    $tempfile = $url;
50    if (url_is_remote($url))
51    {
52      $tempfile = tempnam($path, 'jpg');
53      fetchRemote($url, $tempfile_content);
54      file_put_contents($tempfile, $tempfile_content);
55    }
56
57    list($width, $height, $type) = getimagesize($tempfile);
58    if (IMAGETYPE_PNG == $type)
59    {
60      $srcImage = imagecreatefrompng($tempfile);
61    }
62    else
63    {
64      $srcImage = imagecreatefromjpeg($tempfile);
65    }
66   
67    unlink($tempfile);
68
69    if (isset($_POST['add_band']))
70    {
71      $srcImage = get_image_with_band($srcImage, $gd_ver[0]);
72    }
73
74    $srcWidth = imagesx($srcImage);
75    $srcHeight = imagesy($srcImage);
76
77    if (isset($_POST['thumb_resize']))
78    {
79      $ratioWidth = $srcWidth / $newWidth;
80      $ratioHeight = $srcHeight / $newHeight;
81      if ($ratioWidth < $ratioHeight)
82      {
83        $destWidth = $srcWidth / $ratioHeight;
84        $destHigh = $newHeight;
85      }
86      else
87      {
88        $destWidth = $newWidth;
89        $destHigh = $srcHeight / $ratioWidth;
90      }
91    }
92    else
93    {
94      $destWidth = $srcWidth;
95      $destHigh = $srcHeight;
96    }
97
98    $destImage = imagecreatetruecolor($destWidth, $destHigh);
99   
100    imagecopyresampled(
101      $destImage,
102      $srcImage,
103      0,
104      0,
105      0,
106      0,
107      $destWidth,
108      $destHigh,
109      $srcWidth,
110      $srcHeight
111      );
112
113    if (IMAGETYPE_PNG == $type)
114    {
115      imagepng($destImage, $path);
116    }
117    else
118    {
119      imagejpeg($destImage, $path, 95);
120    }
121   
122    imagedestroy($srcImage);
123    imagedestroy($destImage);
124    return true;
125}
126
127// Récupération de la miniature depuis le serveur
128if ($_POST['thumbnail'] == 'thumb_from_server')
129{
130  $video['thumb_url'] = str_replace('&amp;', '&', $video['thumb_url']);
131  if (($thumb_dir = mkget_thumbnail_dir(dirname($file_path), $error)) == false)
132  {
133    array_push($page['errors'], l10n('py_error9'));
134  }
135  else
136  {
137    $thumb_path = file_path_for_type($file_path, 'thumb');
138    if (file_exists($thumb_path))
139    {
140      array_push($page['errors'], sprintf(l10n('py_error11'), $path), l10n('py_error9'));
141    }
142    else
143    {
144      if (Py_RatioResizeImg($video['thumb_url'], $thumb_path, $_POST['thumb_width'], $_POST['thumb_hight']))
145      {
146        array_push($page['infos'], l10n('py_info2'));
147        $thumb_extension = 'jpg';
148      }
149      else
150      {
151        array_push($page['errors'], l10n('py_error9'));
152      }
153    }
154  }
155}
156
157// Upload de la miniature par l'utilisateur
158if ($_POST['thumbnail'] == 'thumb_from_user')
159{
160  if (empty($_FILES['picture']['size']))
161  {
162    array_push($page['infos'], l10n('py_error12'));
163  }
164  else
165  {
166    $ext = get_extension($_FILES['picture']['name']);
167    if (($thumb_dir = mkget_thumbnail_dir(dirname($file_path), $error)) == false)
168    {
169      array_push($page['errors'], l10n('py_error9'));
170    }
171    else
172    {
173      $thumb_path = get_filename_wo_extension(file_path_for_type($file_path, 'thumb')).'.'.$ext;
174      if (file_exists($thumb_path))
175      {
176        array_push($page['errors'], sprintf(l10n('py_error11'), $thumb_path), l10n('py_error9'));
177      }
178      else
179      {
180        if (Py_RatioResizeImg($_FILES['picture']['tmp_name'], $thumb_path, $_POST['thumb_width'], $_POST['thumb_hight']))
181        {
182          $thumb_extension = $ext;
183          array_push($page['infos'], l10n('py_info2'));
184        }
185        else
186        {
187          array_push($page['errors'], l10n('py_error9'));
188        }
189      }
190    }
191  }
192}
193
194?>
Note: See TracBrowser for help on using the repository browser.