Page 1 of 1

thumbnails script work for frontend not for backend

Posted: Mon Feb 25, 2008 4:28 pm
by piotrekkr
Hi. I have problem with my script that show image thumbnail on my page. In frontend everything works great but not in admin panel.

action.show_thumb_for_advert.php

Code: Select all

<?php
if(!isset($gCms)) exit;
	$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
	$thumb_heigth = $this->GetPreference('thumb_for_advert_height', 80);
	if(empty($params['image_id']) || !$this->CorrectId($params['image_id'])){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 2'); // Niepoprawne ID
		exit;
	}
	$db = &$this->GetDB();
	$query = "SELECT image_name FROM module_FleaMarket_adverts_images WHERE image_id=".$params['image_id'];
	$result = $db->Execute($query);
	if(!$result) die($db->ErrorMsg());
	if($result->NumRows() == 0){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 3'); // Nie istenieje takie ID
		exit;
	}
	$row = $result->FetchRow();
	$adverts_img_dir = realpath(cms_join_path(dirname(__FILE__), '..', '..', 'uploads', 'images', 'fleamarket'));
	if(!$adverts_img_dir || !file_exists(cms_join_path($adverts_img_dir, $row['image_name']))){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 4'); // Nie istnieje taki plik
		exit;
	}
	$image_path = cms_join_path($adverts_img_dir, $row['image_name']);
	$image_info = @ getimagesize($image_path);
	if(empty($image_info)){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 5'); // To nie obrazek
		exit;
	}
	if($image_info[1] > $image_info[0]){
		$percent = round(($this->GetPreference('thumb_for_advert_height', 80) * 100) / $image_info[1], 0);
		$thumb_height = $this->GetPreference('thumb_for_advert_height', 80);
		$thumb_width = ceil(($percent / 100) * $image_info[1]);
	}else if($image_info[0] > $image_info[1]){
		$percent = round(($this->GetPreference('thumb_for_advert_width', 120) * 100) / $image_info[0], 0);
		$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
		$thumb_heigth = ceil(($percent / 100) * $image_info[1]);
	}else{
		$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
		$thumb_heigth = $this->GetPreference('thumb_for_advert_width', 120);
	}
	switch($image_info[2]){
		case IMAGETYPE_GIF : 
			$img_full = @imagecreatefromgif($image_path);
			if(!$img_full){
				$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 6'); // Nie można otworzyć obrazka GIF
				exit;
			}
			
			$img_thumb = imagecreatetruecolor($thumb_width, $thumb_heigth);
			imagecopyresampled($img_thumb, $img_full, 0, 0, 0, 0, $thumb_width, $thumb_heigth, $image_info[0], $image_info[1]);
			if(ini_get('zlib.output_compression')){
				ini_set('zlib.output_compression', 'Off');
			}
			@ob_clean();
			@ob_clean();
			header('Pragma: public');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private', false);
			header('Content-Type: image/gif');
			imagegif($img_thumb);
			exit;
		break;
		
		case IMAGETYPE_JPEG : 
			$img_full = @imagecreatefromjpeg($image_path);
			if(!$img_full){
				$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 7'); // Nie można otworzyć obrazka JPEG
				exit;
			}
			$img_thumb = imagecreatetruecolor($thumb_width, $thumb_heigth);
			imagecopyresampled($img_thumb, $img_full, 0, 0, 0, 0, $thumb_width, $thumb_heigth, $image_info[0], $image_info[1]);
			if(ini_get('zlib.output_compression')){
				ini_set('zlib.output_compression', 'Off');
			}
			@ob_clean();
			@ob_clean();
			header('Pragma: public');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private', false);
			header('Content-Type: image/jpeg');
			imagejpeg($img_thumb);
			exit;
		break;
		
		default: $this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 8'); // To nie jest obrazek typu JPEG lub GIF
		exit;
	}

?>
I transfer links to smarty like that:

Code: Select all

<?php
//....................................
$query = "SELECT image_id FROM module_FleaMarket_adverts_images WHERE advert_id=".$params['advert_id'];
	$result = $db->Execute($query);
	$adver_images = array();
	if($result->NumRows() != 0){
		$i = 0;
		while($row = $result->FetchRow()){
			$advert_images[$i]['image_thumb_link'] = $this->CreateLink($id, 'show_thumb_for_advert', $returnid, '', 
			array('image_id' =>$row['image_id'], 'showtemplate' => 'false'), '', true);
			$advert_images[$i]['image_big_link'] = $this->CreateLink($id, 'show_big_image', $returnid, '', 
			array('image_id' =>$row['image_id'], 'showtemplate' => 'false'), '', true);
			$i++;
		}
	}
	if(empty($advert_images)){
		$this->smarty->assign('advert_images', '');
	}else{
		$this->smarty->assign_by_ref('advert_images', $advert_images);
	}

//...................................
?>
and in my template I use them like this:

Code: Select all

{if !empty($advert_images)}
<div>
	{foreach from=$advert_images item=image}
	<a href="{$image.image_big_link}" target="_blank"><img src="{$image.image_thumb_link}" alt="zdjęcie"  /></a>
	{/foreach}
</div>	
{/if}
and for frontend it works great. In admin panel i use code:

action.admin_adverts_panel.php

Code: Select all

<?php
//............................
$query = "SELECT image_id FROM module_FleaMarket_adverts_images WHERE advert_id=".$row['advert_id'];
		$result = $db->Execute($query);
		if(!$result) die($db->ErrorMsg());
		$adver_images = array();
		if($result->NumRows() != 0){
			$i = 0;
			while($row = $result->FetchRow()){
				$advert_images[$i]['image_thumb_link'] = $this->CreateLink($id, 'show_thumb_for_advert', $returnid, '', 
				array('image_id' =>$row['image_id'], 'showtemplate' => 'false'), '', true, true);
				$advert_images[$i]['image_big_link'] = $this->CreateLink($id, 'show_big_image', $returnid, '', 
				array('image_id' =>$row['image_id'], 'showtemplate' => 'false'), '', true, true);
				$i++;
			}
		}
		if(empty($advert_images)){
			$this->smarty->assign('advert_images', '');
		}else{
			$this->smarty->assign_by_ref('advert_images', $advert_images);
		}
//............................
?>
and template

Code: Select all

{if !empty($advert_images)}
<div>
	{foreach from=$advert_images item=image}
	<a href="{$image.image_big_link}" target="_blank"><img src="{$image.image_thumb_link}" alt="zdjęcie"  /></a>
	{/foreach}
</div>	
{/if}
but images aren't displayed. Generated link in frontend is like this:

Code: Select all

http://domain.com/index.php?mact=FleaMarket,cntnt01,show_thumb_for_advert,0&cntnt01image_id=11&cntnt01showtemplate=false&cntnt01returnid=64
and in admin panel:

Code: Select all

http://domain.com/admin/moduleinterface.php?mact=FleaMarket,m1_,show_thumb_for_advert,1&m1_image_id=3&m1_showtemplate=false
there is something wrong with .../admin/... in backend link but I don't know what.
And this is my thumbnail script:

Code: Select all

<?php
if(!isset($gCms)) exit;
	$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
	$thumb_heigth = $this->GetPreference('thumb_for_advert_height', 80);
	if(empty($params['image_id']) || !$this->CorrectId($params['image_id'])){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 2'); // Niepoprawne ID
		exit;
	}
	$db = &$this->GetDB();
	$query = "SELECT image_name FROM module_FleaMarket_adverts_images WHERE image_id=".$params['image_id'];
	$result = $db->Execute($query);
	if(!$result) die($db->ErrorMsg());
	if($result->NumRows() == 0){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 3'); // Nie istenieje takie ID
		exit;
	}
	$row = $result->FetchRow();
	$adverts_img_dir = realpath(cms_join_path(dirname(__FILE__), '..', '..', 'uploads', 'images', 'fleamarket'));
	if(!$adverts_img_dir || !file_exists(cms_join_path($adverts_img_dir, $row['image_name']))){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 4'); // Nie istnieje taki plik
		exit;
	}
	$image_path = cms_join_path($adverts_img_dir, $row['image_name']);
	$image_info = @ getimagesize($image_path);
	if(empty($image_info)){
		$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 5'); // To nie obrazek
		exit;
	}
	if($image_info[1] > $image_info[0]){
		$percent = round(($this->GetPreference('thumb_for_advert_height', 80) * 100) / $image_info[1], 0);
		$thumb_height = $this->GetPreference('thumb_for_advert_height', 80);
		$thumb_width = ceil(($percent / 100) * $image_info[1]);
	}else if($image_info[0] > $image_info[1]){
		$percent = round(($this->GetPreference('thumb_for_advert_width', 120) * 100) / $image_info[0], 0);
		$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
		$thumb_heigth = ceil(($percent / 100) * $image_info[1]);
	}else{
		$thumb_width = $this->GetPreference('thumb_for_advert_width', 120);
		$thumb_heigth = $this->GetPreference('thumb_for_advert_width', 120);
	}
	switch($image_info[2]){
		case IMAGETYPE_GIF : 
			$img_full = @imagecreatefromgif($image_path);
			if(!$img_full){
				$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 6'); // Nie można otworzyć obrazka GIF
				exit;
			}
			
			$img_thumb = imagecreatetruecolor($thumb_width, $thumb_heigth);
			imagecopyresampled($img_thumb, $img_full, 0, 0, 0, 0, $thumb_width, $thumb_heigth, $image_info[0], $image_info[1]);
			if(ini_get('zlib.output_compression')){
				ini_set('zlib.output_compression', 'Off');
			}
			@ob_clean();
			@ob_clean();
			header('Pragma: public');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private', false);
			header('Content-Type: image/gif');
			imagegif($img_thumb);
			exit;
		break;
		
		case IMAGETYPE_JPEG : 
			$img_full = @imagecreatefromjpeg($image_path);
			if(!$img_full){
				$this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 7'); // Nie można otworzyć obrazka JPEG
				exit;
			}
			$img_thumb = imagecreatetruecolor($thumb_width, $thumb_heigth);
			imagecopyresampled($img_thumb, $img_full, 0, 0, 0, 0, $thumb_width, $thumb_heigth, $image_info[0], $image_info[1]);
			if(ini_get('zlib.output_compression')){
				ini_set('zlib.output_compression', 'Off');
			}
			@ob_clean();
			@ob_clean();
			header('Pragma: public');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private', false);
			header('Content-Type: image/jpeg');
			imagejpeg($img_thumb);
			exit;
		break;
		
		default: $this->CreateErrorThumb($thumb_width, $thumb_heigth, 'Error: 8'); // To nie jest obrazek typu JPEG lub GIF
		exit;
	}

?>
Plz help :) Thanks

Re: thumbnails script work for frontend not for backend

Posted: Tue Feb 26, 2008 7:11 pm
by piotrekkr
Ok maby i should ask how can I make link in admin panel that would be the same as created in frontend action. If I use the same CreateLink() function in frontend and in backend the links are different. Is there any function used in admin panel that could make link like in frontend? I tried to use CreateFrontEndLink() but it didn't work. Please help.

PS. sorry for my poor english