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

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

feature 1941: make PY Gvideo plugin compatible with virtual categories (and remote sites as well)

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    $ver_info = gd_info();
50    preg_match('/\d/', $ver_info['GD Version'], $gd_ver);
51
52    if (!($tempfile = @tempnam( $path, 'jpg')
53      and $handle = @fopen($tempfile, 'wb')
54      and fetchRemote($url, $handle)
55      and @fclose($handle)
56      and $srcImage = @imagecreatefromjpeg($tempfile)))
57    {
58      return false;
59    }
60    @unlink($tempfile);
61
62    if (isset($_POST['add_band']))
63    {
64      $srcImage = get_image_with_band($srcImage, $gd_ver[0]);
65    }
66
67    $srcWidth = imagesx($srcImage);
68    $srcHeight = imagesy($srcImage);
69
70    if (isset($_POST['thumb_resize']))
71    {
72      $ratioWidth = $srcWidth / $newWidth;
73      $ratioHeight = $srcHeight / $newHeight;
74      if ($ratioWidth < $ratioHeight)
75      {
76        $destWidth = $srcWidth / $ratioHeight;
77        $destHigh = $newHeight;
78      }
79      else
80      {
81        $destWidth = $newWidth;
82        $destHigh = $srcHeight / $ratioWidth;
83      }
84    }
85    else
86    {
87      $destWidth = $srcWidth;
88      $destHigh = $srcHeight;
89    }
90
91    if ($gd_ver[0] >= 2)
92    {
93        $destImage = imagecreatetruecolor($destWidth, $destHigh);
94        imagecopyresampled($destImage, $srcImage, 0, 0, 0, 0,
95            $destWidth, $destHigh, $srcWidth, $srcHeight);
96    }
97    else
98    {
99        $destImage = imagecreate($destWidth, $destHigh);
100        imagecopyresized($destImage, $srcImage, 0, 0, 0, 0,
101            $destWidth, $destHigh, $srcWidth, $srcHeight);
102    }
103
104    imagejpeg($destImage, $path);
105    imagedestroy($srcImage);
106    imagedestroy($destImage);
107    return true;
108}
109
110// Récupération de la miniature depuis le serveur
111if ($_POST['thumbnail'] == 'thumb_from_server')
112{
113  $video['thumb_url'] = str_replace('&amp;', '&', $video['thumb_url']);
114  if (($thumb_dir = mkget_thumbnail_dir(dirname($file_path), $error)) == false)
115  {
116    array_push($page['errors'], l10n('py_error9'));
117  }
118  else
119  {
120    $thumb_path = file_path_for_type($file_path, 'thumb');
121    if (file_exists($thumb_path))
122    {
123      array_push($page['errors'], sprintf(l10n('py_error11'), $path), l10n('py_error9'));
124    }
125    else
126    {
127      if (Py_RatioResizeImg($video['thumb_url'], $thumb_path, $_POST['thumb_width'], $_POST['thumb_hight']))
128      {
129        array_push($page['infos'], l10n('py_info2'));
130        $thumb_extension = 'jpg';
131      }
132      else
133      {
134        array_push($page['errors'], l10n('py_error9'));
135      }
136    }
137  }
138}
139
140// Upload de la miniature par l'utilisateur
141if ($_POST['thumbnail'] == 'thumb_from_user')
142{
143  if (empty($_FILES['picture']['size']))
144  {
145    array_push($page['infos'], l10n('py_error12'));
146  }
147  else
148  {
149    $ext = get_extension($_FILES['picture']['name']);
150    if (($thumb_dir = mkget_thumbnail_dir(dirname($file_path), $error)) == false)
151    {
152      array_push($page['errors'], l10n('py_error9'));
153    }
154    else
155    {
156      $thumb_path = get_filename_wo_extension(file_path_for_type($file_path, 'thumb')).'.'.$ext;
157      if (file_exists($thumb_path))
158      {
159        array_push($page['errors'], sprintf(l10n('py_error11'), $thumb_path), l10n('py_error9'));
160      }
161      else
162      {
163        if (Py_RatioResizeImg($_FILES['picture']['tmp_name'], $thumb_path, $_POST['thumb_width'], $_POST['thumb_hight']))
164        {
165          $thumb_extension = $ext;
166          array_push($page['infos'], l10n('py_info2'));
167        }
168        else
169        {
170          array_push($page['errors'], l10n('py_error9'));
171        }
172      }
173    }
174  }
175}
176
177?>
Note: See TracBrowser for help on using the repository browser.