Prado Framework : Upload Picture


Form preview with TActiveFileUpload

upload_picture_prado

 

 

 

 

 

 

test.page:

Code:
<com:TActiveFileUpload ID="actUpload" MaxFileSize="102400" OnFileUpload="onImgUpload" />
<com:TActiveImage ID="labPhoto />
<com:TActiveLabel ID="labError />
test.php:

Code:
<?php

public function onImgUpload($sender, $param)
{
	if(!extension_loaded('gd'))
	{
		$this->labError->Text="Error: missing php's gd extension.";
		return;
	}

	if (!$sender->getHasFile())
	{
		//error handling
		switch ($sender->ErrorCode)
		{
			case 1:
				$err="file size too big (php.ini).";
				break;
			case 2:
				$err="file size too big (form).";
				break;
			case 3:
				$err="file upload interrupted.";
				break;
			case 4:
				$err="no file chosen.";
				break;
			case 6:
				$err="internal problem (missing temporary directory).";
				break;
			case 7:
				$err="unable to write file on disk.";
				break;
			case 8:
				$err="file type not accepted.";
				break;
		}

		$this->labError->Text="Error: ".$err;
		return;
	}

	$mime=$sender->getFileType();
	if($mime!="image/png" && $mime!="image/jpg" && $mime!="image/jpeg")
	{
		$this->labError->Text="Error: the file is not an image";
		return;
	}

	$source=false;

	if($mime=="image/png")
	{
		if(!(imagetypes() & IMG_PNG))
		{
			$this->labError->Text="Error: missing png support in gd library.";
			return;
		}
		$source = imagecreatefrompng($sender->LocalName);
	}

	if(($mime=="image/jpg" || $mime=="image/jpeg"))
	{
		if(!(imagetypes() & IMG_JPG))
		{
			$this->labError->Text="Error: missing jpeg support in gd library.";
			return;
		}
		$source = imagecreatefromjpeg($sender->LocalName);
	}

	if(!$source)
	{
		$this->labError->Text="Errore: invalid file.");
		return;
	}

	list($width_orig, $height_orig) = getimagesize($sender->LocalName);
	$width = 80;
	$height = 80;

	$thumb = imagecreatetruecolor($width, $height);

	// Get new dimensions

	$ratio_orig = $width_orig/$height_orig;

	if ($width/$height > $ratio_orig) {
		$width = $height*$ratio_orig;
	} else {
		$height = $width/$ratio_orig;
	}
	imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

	// use output buffering to capture outputted image stream
	ob_start();
	imagejpeg($thumb);
	$thumbjpg = ob_get_clean();

	$this->labPhoto->ImageUrl = "data:image/jpeg;base64,".base64_encode($thumbjpg);

            $new_filename = $sender->FileName;
            $path = "/opt/lampp/htdocs/your_app/protected/Pages/app/img/".$new_filename;
            // to save file into drive/folder
            if($sender->saveAs($path,false)){
                $this->photo_url->Text = $path;
            }
}

About blackphp

All about programming php, ajax, jquery, mysql, etc, tips n trick. Now, i'm concern in php programming with codeigniter framework n also training my design skill with photoshop or corel... wish me luck! ('o')

Posted on November 4, 2013, in Prado Framework and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: