Project

General

Profile

Bug #2185 » resizePicture.patch

Simon Legner, 19/10/2010 18:04

View differences:

main/exercice/hotspot.class.php Tue Oct 19 12:26:29 2010 +0300 → main/exercice/hotspot.class.php Tue Oct 19 18:04:05 2010 +0200
66 66
		if(!empty($file_info['tmp_name'])) {
67 67
			$this->uploadPicture($file_info['tmp_name'], $file_info['name']);
68 68
			global $picturePath;
69
			//fixed width ang height 
70 69
			if (file_exists($picturePath.'/'.$this->picture)) { 
71
				//list($width,$height) = @getimagesize($file_info['tmp_name']); does not work	
72
				list($width,$height) = @getimagesize($picturePath.'/'.$this->picture);				
73
				if($width>$height) {
74
					$this->resizePicture('width',545);
75
				} else {
76
					$this->resizePicture('height',350);
77
				}
70
				$this->resizePicture(545, 350);
78 71
				$this->save();			
79 72
			} else {
80 73
				return false;
main/exercice/question.class.php Tue Oct 19 12:26:29 2010 +0300 → main/exercice/question.class.php Tue Oct 19 18:04:05 2010 +0200
369 369
	 * @param - integer $Max - Maximum size
370 370
	 * @return - boolean - true if success, false if failed
371 371
	 */
372
	function resizePicture($Dimension, $Max)
372
	function resizePicture($target_width, $target_height)
373 373
	{
374 374
		global $picturePath;
375 375

  
376 376
		// if the question has an ID
377 377
		if($this->id) {
378
	  		// Get dimensions from current image.
379 378
	  		$current_img = imagecreatefromjpeg($picturePath.'/'.$this->picture);
380 379

  
380
	  		// Get dimensions from current image.
381 381
	  		$current_image_size = getimagesize($picturePath.'/'.$this->picture);
382
	  		$current_height = imagesy($current_img);
383
			$current_width = imagesx($current_img);
382
	  		$current_width = $current_image_size[0];
383
			$current_height = $current_image_size[1];
384 384

  
385
			if($current_image_size[0] < $Max && $current_image_size[1] <$Max)
386
				return true;
387
			elseif($current_height == "")
388
				return false;
389

  
390
			// Resize according to height.
391
			if ($Dimension == "height")
392
			{
393
				$resize_scale = $current_height / $Max;
394
				$new_height = $Max;
395
				$new_width = ceil($current_width / $resize_scale);
396
			}
397

  
398
			// Resize according to width
399
			if ($Dimension == "width")
400
			{
401
				$resize_scale = $current_width / $Max;
402
				$new_width = $Max;
403
				$new_height = ceil($current_height / $resize_scale);
404
			}
405

  
406
			// Resize according to height or width, both should not be larger than $Max after resizing.
407
			if ($Dimension == "any")
408
			{
409
				if ($current_height > $current_width || $current_height == $current_width)
410
				{
411
					$resize_scale = $current_height / $Max;
412
					$new_height = $Max;
413
					$new_width = ceil($current_width / $resize_scale);
414
				}
415
				if ($current_height < $current_width)
416
				{
417
					$resize_scale = $current_width / $Max;
418
					$new_width = $Max;
419
					$new_height = ceil($current_height / $resize_scale);
420
				}
421
			}
422

  
423
			// Create new image
424
		    $new_img = imagecreatetruecolor($new_width, $new_height);
385
			$new_img = imagecreatetruecolor($target_width, $target_height);
425 386
			$bgColor = imagecolorallocate($new_img, 255,255,255);
426 387
			imagefill($new_img , 0,0 , $bgColor);
427 388

  
389
			if($current_width==$target_width && $current_height==$target_height) {
390
				// no modification needed
391
				return true;
392
			} else if($current_width<=$target_width && $current_height<=$target_height) {
393
				// extend canvas with white background
394
				$new_width = $current_width;
395
				$new_height = $current_height;
396
			} else {
397
				// resize image
398
				$width_ratio = 1. * $target_width / $current_width;
399
				$height_ratio = 1. * $target_height / $current_height;
400
				$ratio_min = min($width_ratio, $height_ratio);
401
				
402
				$new_width = (int) ($current_width * $ratio_min);
403
				$new_height = (int) ($current_height * $ratio_min);
404
				
405
				$dstX = 0;
406
				$dstY = 0;
407
			}
408
			
409
			// calculate left upper corner of resized image in canvas
410
			$dstX = (int) (($target_width - $new_width)/2);
411
			$dstY = (int) (($target_height - $new_height)/2);
412
			
428 413
			// Resize image
429
			imagecopyresized($new_img, $current_img, 0, 0, 0, 0, $new_width, $new_height, $current_width, $current_height);
414
			imagecopyresized($new_img, $current_img, $dstX, $dstY, 0, 0, $new_width, $new_height, $current_width, $current_height);
430 415

  
431 416
			// Write image to file
432
		    $result = imagejpeg($new_img, $picturePath.'/'.$this->picture, 100);
417
		    $result = imagejpeg($new_img, $picturePath.'/'.$this->picture, 90);
433 418

  
434 419
		    // Delete temperory images, clear memory
435 420
			imagedestroy($current_img);
436 421
			imagedestroy($new_img);
437 422

  
438
			if ($result)
439
			{
440
				return true;
423
			return $result;
441 424
			}
442
			else
443
			{
444
				return false;
445
			}
446
		}
447

  
448

  
449 425
	}
450 426

  
451 427
	/**
(1-1/5)