Feature #306 - Replacing the ugly construct (my workaround) @getimagesize(api_url_to_local_path($image_path)) with a better one: api_getimagesize($image_path).

skala
Ivan Tcholakov 16 years ago
parent 5ed7f167b5
commit e197cb109e
  1. 4
      main/admin/user_edit.php
  2. 2
      main/auth/profile.php
  3. 2
      main/forum/forumfunction.inc.php
  4. 2
      main/forum/index.php
  5. 321
      main/inc/lib/image.lib.php
  6. 24
      main/inc/lib/main_api.lib.php
  7. 4
      main/inc/lib/usermanager.lib.php
  8. 2
      main/mySpace/myStudents.php
  9. 2
      main/newscorm/openoffice_text_document.class.php
  10. 4
      main/user/userInfo.php
  11. 9
      tests/main/inc/lib/main_api.lib.test.php
  12. 2
      whoisonline.php

@ -438,7 +438,7 @@ $image_path = UserManager::get_user_picture_path_by_id($user_id,'web');
$image_dir = $image_path['dir'];
$image = $image_path['file'];
$image_file = ($image != '' ? $image_dir.$image : api_get_path(WEB_CODE_PATH).'img/unknown.jpg');
$image_size = @getimagesize(api_url_to_local_path($image_file));
$image_size = api_getimagesize($image_file);
$img_attributes = 'src="'.$image_file.'?rand='.time().'" '
.'alt="'.api_get_person_name($user_data['firstname'], $user_data['lastname']).'" '
@ -450,7 +450,7 @@ if ($image_size[0] > 300) { //limit display width to 300px
// get the path,width and height from original picture
$big_image = $image_dir.'big_'.$image;
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width = $big_image_size[0];
$big_image_height = $big_image_size[1];
$url_big_image = $big_image.'?rnd='.time();

@ -782,7 +782,7 @@ if ($image_size[0] > 300) {
// get the path,width and height from original picture
$big_image = $image_dir.'big_'.$image;
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width = $big_image_size[0];
$big_image_height = $big_image_size[1];
$url_big_image = $big_image.'?rnd='.time();

@ -310,7 +310,7 @@ function show_add_forum_form($inputvalues=array()) {
if (isset($inputvalues['forum_image']) && strlen($inputvalues['forum_image']) > 0) {
$image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$inputvalues['forum_image'];
$image_size = @getimagesize(api_url_to_local_path($image_path));
$image_size = api_getimagesize($image_path);
$img_attributes = '';
if (!empty($image_size)) {

@ -348,7 +348,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'notify' AND isset($_GET['conte
if(!empty($forum['forum_image'])) {
$image_path = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$forum['forum_image'];
$image_size = @getimagesize(api_url_to_local_path($image_path));
$image_size = api_getimagesize($image_path);
$img_attributes = '';
if (!empty($image_size)) {

@ -1,166 +1,155 @@
<?php
class image {
var $bg;
var $logo;
var $logox;
var $logoy;
var $bgx;
var $bgy;
var $fontfile='./verdana';
var $color;
function image($bgfile='') {
image::addbackground($bgfile);
}
function createimagefromtype($file,$handler) {
$size = @getimagesize(api_url_to_local_path($file));
$type=$size[2];
switch ($type) {
case 2 : $imhandler = @imagecreatefromjpeg($file);
break;
case 3 : $imhandler = @imagecreatefrompng($file);
break;
case 1 : $imhandler = @imagecreatefromgif($file);
break;
}
$xtmpstr=$handler.'x';
$ytmpstr=$handler.'y';
$this->$xtmpstr=$size[0];
$this->$ytmpstr=$size[1];
return $imhandler;
}
function resize ( $thumbw , $thumbh , $border) {
$size [0]=$this->bgx;
$size [1]=$this->bgy;
if ($border==1) {
$scale = min($thumbw/$size[0], $thumbh/$size[1]);
$width = (int)($size[0]*$scale);
$height = (int)($size[1]*$scale);
$deltaw = (int)(($thumbw - $width)/2);
$deltah = (int)(($thumbh - $height)/2);
$dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
if ( !empty($this->color))
@imagefill ( $dst_img, 0, 0, $this->color );
$this->bgx=$thumbw;
$this->bgy=$thumbh;
}
elseif ($border==0) {
$scale = ($size[0] > 0 && $size[1] >0)?min($thumbw/$size[0], $thumbh/$size[1]):0;
$width = (int)($size[0]*$scale);
$height = (int)($size[1]*$scale);
$deltaw = 0;
$deltah = 0;
$dst_img = @ImageCreateTrueColor($width, $height);
$this->bgx=$width;
$this->bgy=$height;
}
$src_img = $this->bg;
@ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img),ImageSY($src_img));
$this->bg=$dst_img;
@imagedestroy($src_img);
}
function addbackground($bgfile) {
if ( !empty($bgfile) && file_exists($bgfile) ) { $this->bg = image::createimagefromtype($bgfile,'bg');
@imagealphablending( $this->bg ,TRUE );
}
}
function addlogo($file) {
$this->logo = image::createimagefromtype($file,'logo');
@imagealphablending( $this->logo ,TRUE );
$size = @getimagesize(api_url_to_local_path($file));
$this->logox=$size[0];
$this->logoy=$size[1];
}
function mergelogo($x,$y,$alpha=100) {
if ($x<0) $x=$this->bgx-$this->logox+$x;
if ($y<0) $y=$this->bgy-$this->logoy+$y;
return @imagecopymerge ( $this->bg, $this->logo, $x, $y, 0, 0, $this->logox, $this->logoy, $alpha );
}
function makecolor($red, $green, $blue) {
$this->color = @imagecolorallocate( $this->bg, $red, $green, $blue );
}
function setfont($fontfile) {
$this->fontfile=$fontfile;
}
function addtext ($text,$x=0,$y=0,$size=12,$angle=0) {
putenv('GDFONTPATH=' . realpath('.'));
$this->fontfile='verdana';
$text= preg_replace('`(?<!\r)\n`',"\r\n",$text);
$box=@imagettfbbox ( $size, $angle, $this->fontfile, $text );
if ($x<0) {
$x=$this->bgx - max($box[2],$box[4]) + $x;
} else {
$x=max(-$box[0],-$box[6]) + $x;
}
if ($y<0) {
$y=$this->bgy - max($box[1],$box[3]) + $y;
} else {
$y=max(-$box[7],-$box[5]) + $y;
}
@imagettftext($this->bg, $size, $angle, $x, $y, $this->color, $this->fontfile , $text );
}
function send_image($type,$file='',$compress=-1) {
switch ($type) {
case 'JPG' :
if (!$file) header("Content-type: image/jpeg");
if ($compress==-1) $compress=100;
return imagejpeg($this->bg,$file,$compress);
break;
case 'PNG' :
if (!$file) header("Content-type: image/png");
if ($compress!=-1)
@imagetruecolortopalette ( $this->bg, true, $compress);
return imagepng($this->bg,$file,$compress);
break;
case 'GIF' :
if (!$file) header("Content-type: image/gif");
if ($compress!=-1)
@imagetruecolortopalette ( $this->bg, true, $compress);
return imagegif($this->bg,$file,$compress);
break;
default: return 0;
}
// TODO: Occupied memory is not released, because the following fragment of code is actually dead.
@imagedestroy($this->bg);
@imagedestroy($this->logo);
}
}
?>
<?php
class image {
var $bg;
var $logo;
var $logox;
var $logoy;
var $bgx;
var $bgy;
var $fontfile = './verdana';
var $color;
function image($bgfile = '') {
image::addbackground($bgfile);
}
function createimagefromtype($file, $handler) {
$size = api_getimagesize($file);
$type = $size[2];
switch ($type) {
case 2 : $imhandler = @imagecreatefromjpeg($file); break;
case 3 : $imhandler = @imagecreatefrompng($file); break;
case 1 : $imhandler = @imagecreatefromgif($file); break;
}
$xtmpstr = $handler.'x';
$ytmpstr = $handler.'y';
$this->$xtmpstr = $size[0];
$this->$ytmpstr = $size[1];
return $imhandler;
}
function resize($thumbw, $thumbh, $border) {
$size [0] = $this->bgx;
$size [1] = $this->bgy;
if ($border == 1) {
$scale = min($thumbw / $size[0], $thumbh / $size[1]);
$width = (int)($size[0] * $scale);
$height = (int)($size[1] * $scale);
$deltaw = (int)(($thumbw - $width) / 2);
$deltah = (int)(($thumbh - $height) / 2);
$dst_img = @ImageCreateTrueColor($thumbw, $thumbh);
if (!empty($this->color)) {
@imagefill($dst_img, 0, 0, $this->color);
}
$this->bgx = $thumbw;
$this->bgy = $thumbh;
}
elseif ($border == 0) {
$scale = ($size[0] > 0 && $size[1] > 0) ? min($thumbw / $size[0], $thumbh / $size[1]) : 0;
$width = (int)($size[0] * $scale);
$height = (int)($size[1] * $scale);
$deltaw = 0;
$deltah = 0;
$dst_img = @ImageCreateTrueColor($width, $height);
$this->bgx = $width;
$this->bgy = $height;
}
$src_img = $this->bg;
@ImageCopyResampled($dst_img, $src_img, $deltaw, $deltah, 0, 0, $width, $height, ImageSX($src_img), ImageSY($src_img));
$this->bg=$dst_img;
@imagedestroy($src_img);
}
function addbackground($bgfile) {
if (!empty($bgfile) && file_exists($bgfile)) {
$this->bg = image::createimagefromtype($bgfile, 'bg');
@imagealphablending($this->bg, true);
}
}
function addlogo($file) {
$this->logo = image::createimagefromtype($file, 'logo');
@imagealphablending($this->logo , true);
$size = api_getimagesize($file);
$this->logox = $size[0];
$this->logoy = $size[1];
}
function mergelogo($x, $y, $alpha = 100) {
if ($x < 0) $x = $this->bgx - $this->logox + $x;
if ($y < 0) $y = $this->bgy - $this->logoy + $y;
return @imagecopymerge($this->bg, $this->logo, $x, $y, 0, 0, $this->logox, $this->logoy, $alpha);
}
function makecolor($red, $green, $blue) {
$this->color = @imagecolorallocate($this->bg, $red, $green, $blue);
}
function setfont($fontfile) {
$this->fontfile = $fontfile;
}
function addtext ($text, $x = 0, $y = 0, $size = 12, $angle = 0) {
putenv('GDFONTPATH=' . realpath('.'));
$this->fontfile='verdana';
$text= preg_replace('`(?<!\r)\n`', "\r\n", $text);
$box = @imagettfbbox($size, $angle, $this->fontfile, $text);
if ($x < 0) {
$x = $this->bgx - max($box[2], $box[4]) + $x;
} else {
$x = max(-$box[0], -$box[6]) + $x;
}
if ($y < 0) {
$y = $this->bgy - max($box[1], $box[3]) + $y;
} else {
$y = max(-$box[7], -$box[5]) + $y;
}
@imagettftext($this->bg, $size, $angle, $x, $y, $this->color, $this->fontfile , $text);
}
function send_image($type, $file = '', $compress = -1) {
switch ($type) {
case 'JPG':
if (!$file) header("Content-type: image/jpeg");
if ($compress == -1) $compress = 100;
return imagejpeg($this->bg, $file, $compress);
case 'PNG':
if (!$file) header("Content-type: image/png");
if ($compress != -1) {
@imagetruecolortopalette($this->bg, true, $compress);
}
return imagepng($this->bg, $file, $compress);
case 'GIF':
if (!$file) header("Content-type: image/gif");
if ($compress != -1) {
@imagetruecolortopalette($this->bg, true, $compress);
}
return imagegif($this->bg, $file, $compress);
default: return 0;
}
// TODO: Occupied memory is not released, because the following fragment of code is actually dead.
@imagedestroy($this->bg);
@imagedestroy($this->logo);
}
}

@ -397,7 +397,8 @@ function api_get_path($path_type, $path = null) {
// http://localhost/dokeos/courses/TEST/document/image.png
if (preg_match('/(.*)main\/document\/download.php\?doc_url=\/(.*)&cDir=\/(.*)?/', $path, $matches)) {
global $_cid, $_course;
if (!empty($_cid) && $_cid != -1 && isset($_course)) { // Inside a course?
if (!empty($_cid) && $_cid != -1 && isset($_course)) { // Inside a course? // TODO: This restriction to be revised, what about converting
// links which were created while content was within another course?
$path = $matches[1].'courses/'.$_course['path'].'/document/'.str_replace('//', '/', $matches[3].'/'.$matches[2]);
} else {
return $original_path; // Not inside a course, return then the URL "as is".
@ -3423,6 +3424,16 @@ function api_is_windows_os() {
return strtolower(substr((string)$os, 0, 3 )) == 'win';
}
/**
* This wrapper function has been implemented for avoiding some known problems about the function getimagesize().
* @link http://php.net/manual/en/function.getimagesize.php
* @link http://www.dokeos.com/forum/viewtopic.php?t=12345
* @link http://www.dokeos.com/forum/viewtopic.php?t=16355
*/
function api_getimagesize($path) {
return @getimagesize(api_get_path(TO_SYS_PATH, $path));
}
/**
* This function resizes an image, with preserving its proportions (or aspect ratio).
* @author Ivan Tcholakov, MAY-2009.
@ -3432,7 +3443,7 @@ function api_is_windows_os() {
* @return array Calculated new width and height
*/
function api_resize_image($image, $target_width, $target_height) {
$image_properties = @getimagesize(api_url_to_local_path($image)); // We have to call getimagesize() in a safe way.
$image_properties = api_getimagesize($image);
$image_width = $image_properties[0];
$image_height = $image_properties[1];
return api_calculate_image_size($image_width, $image_height, $target_width, $target_height);
@ -3576,15 +3587,6 @@ function is_allowed_to_edit() {
/**
* @deprecated 19-SEP-2009: Use api_get_path(TO_SYS_PATH, $url) instead.
* This function converts URLs into local file names.
* Purpose: To help diagnosing or making workarounds concerning problems, caused by getimagesize().
* Usage:
* $imagesize = @getimagesize($image); ----> $imagesize = @getimagesize(api_url_to_local_path($image));
* @author Ivan Tcholakov, 23-SEP-2008, the first implementation for Dokeos 1.8.6.
* @param string $url The input URL.
* @return string Returns a system path that is correspondet to the given URL.
* Note: If the given URL has not been detected as absolute URL inside the platform (i.e. if it is a system path,
* a relative path or a URL that points somewhere else in the Web, then it is returned unmodified.
*/
function api_url_to_local_path($url) {
return api_get_path(TO_SYS_PATH, $url);

@ -1820,7 +1820,7 @@ class UserManager {
require_once api_get_path(LIBRARY_PATH).'image.lib.php';
}
$temp = new image($file);
$picture_infos = @getimagesize(api_url_to_local_path($file));
$picture_infos = api_getimagesize($file);
if ($picture_infos[0] > $max_size_for_picture) {
$thumbwidth = $max_size_for_picture;
if (empty($thumbwidth) or $thumbwidth == 0) {
@ -1858,7 +1858,7 @@ class UserManager {
$picture['file'] = $image_array['dir'].$size_picture.$picture_file;
$picture['style'] = '';
if ($height > 0) {
$dimension = @getimagesize(api_url_to_local_path($picture['file']));
$dimension = api_getimagesize($picture['file']);
$margin = (($height - $dimension[1]) / 2);
$picture['style'] = ' style="padding-top:'.$margin.'px; width:'.$dimension[0].'px; height:'.$dimension[1].';" ';
}

@ -338,7 +338,7 @@ if (!empty ($_GET['student'])) {
// get the path,width and height from original picture
$image_file = $image_array['dir'] . $image_array['file'];
$big_image = $image_array['dir'] . 'big_' . $image_array['file'];
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width = $big_image_size[0];
$big_image_height = $big_image_size[1];
$url_big_image = $big_image . '?rnd=' . time();

@ -319,7 +319,7 @@ class OpenOfficeTextDocument extends OpenofficeDocument {
if(!$defined_width)
{
list($img_width, $img_height, $type) = @getimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
list($img_width, $img_height, $type) = api_getimagesize($this->base_work_dir.$this->created_dir.'/'.$image);
$new_width = $max_width-10;
if($img_width > $new_width)

@ -391,7 +391,7 @@ elseif ($displayMode == "viewMainInfoEdit")
$image_array=UserManager::get_user_picture_path_by_id($userIdViewed,'web',false,true);
// get the path,width and height from original picture
$big_image = $image_array['dir'].'big_'.$image_array['file'];
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width= $big_image_size[0];
$big_image_height= $big_image_size[1];
$url_big_image = $big_image.'?rnd='.time();
@ -465,7 +465,7 @@ elseif ($displayMode == "viewContentList") // default display
$image_array=UserManager::get_user_picture_path_by_id($userIdViewed,'web',false,true);
// get the path,width and height from original picture
$big_image = $image_array['dir'].'big_'.$image_array['file'];
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width= $big_image_size[0];
$big_image_height= $big_image_size[1];
$url_big_image = $big_image.'?rnd='.time();

@ -951,15 +951,6 @@ class TestMainApi extends UnitTestCase {
//var_dump($res);
}
function testApiUrlToLocalPath(){
global $url;
$cond= preg_match(false);
$res= api_url_to_local_path($url);
$this->assertTrue(is_null($res));
$this->assertFalse($cond);
//var_dump($res);
}
function testApiResizeImage(){
//global $image, $target_width, $target_height;
$resize = array('image' =>'image.jpg', 'target_width'=>100, 'target_height'=>100);

@ -220,7 +220,7 @@ function display_individual_user($user_id)
$window_name = 'window'.uniqid('');
// get the path,width and height from original picture
$big_image = $webdir.'big_'.$user_object->picture_uri;
$big_image_size = @getimagesize(api_url_to_local_path($big_image));
$big_image_size = api_getimagesize($big_image);
$big_image_width= $big_image_size[0];
$big_image_height= $big_image_size[1];
$url_big_image = $big_image.'?rnd='.time();

Loading…
Cancel
Save