Jump to content

why width and height of image is not able to get in php


rahulmehta

Recommended Posts

i am uploading image to s3 server and by php i am making a copy of that image on server , if i upload an image of 2.3 mb than the width of image is not coming but if i upload less size image like 26kb than it is showing the width of image so it is able to create the copy .

 

here is my code of php :

$s3 = new S3(awsAccessKey, awsSecretKey);

        $thumbId = uniqid();
        $thumbId .= ".jpg"; 
        $img = '';

        if($imgType == "image/jpeg"){
            $img = imagecreatefromjpeg($sourceUrl);
        }else if($imgType == "image/png"){
            $img = imagecreatefrompng($sourceUrl);
        }else if($imgType == "image/gif"){
            $img = imagecreatefromgif($sourceUrl);
        }else{
            $img = imagejpeg($sourceUrl);
        }

    echo    $width = imagesx( $img );
        echo $height = imagesy( $img );

please tell me what is the problem with size of image..

 

regards

 

rahul

 

 

 

If I read your question correctly, You might be running up against your php.ini setting for upload_max_filesize. I think 2mb is the limit by default. You can probably override this if your host allows it (again if I am reading your question correctly).

<?php

function save($path){
	$input = fopen("php://input", "r");
	$fp = fopen($path, "w");
	while ($data = fread($input, 1024)){
		fwrite($fp,$data);
	}
	fclose($fp);
	fclose($input);			
}

// Get file name - eg: myphoto.jpg
function getName(){
	return $_GET['qqfile'];			
}

// Get all params passed from onSubmit()
function getParam($p='all'){
	if ($p=='all') {
		return $_GET;
	} else {
		return $_GET[$p];
	}
}

// Get file size of file
function getSize(){
	$headers = apache_request_headers();
	return (int)$headers['Content-Length'];
}

// Get MIME type of file
function getFileType(){
	$headers = apache_request_headers();
	return (string)$headers['Content-Type'];
}

/* Function to generate thumbnails of uploaded images
 * 
 * @param string $thumbWidth Width of the thumbnail to be created
 * @param string $sourceUrl Location of temp file
 * @param string $imgType Image MIME type
 * @param string $bucket Bucket
 * @param string $cloudFront Link to Cloudfront server
 * @return string $thumbLink Link to generated thumbnail
 */ 
function imgThumbs($minSize, $sourceUrl, $imgType, $bucket, $cloudFront){	

	$s3 = new S3(awsAccessKey, awsSecretKey);


	$thumbId = uniqid();
	$thumbId .= ".jpg";	
	$img = '';

	if($imgType == "image/jpeg"){
		$img = imagecreatefromjpeg($sourceUrl);
	}else if($imgType == "image/png"){
		$img = imagecreatefrompng($sourceUrl);
	}else if($imgType == "image/gif"){
		$img = imagecreatefromgif($sourceUrl);
	}else{
		$img = imagejpeg($sourceUrl);
	}

	$width = imagesx( $img );
	$height = imagesy( $img );

	if($minSize == '') {
		$minSize = 100;
		$newWidth = $minSize;
		$newHeight = $minSize;			
	}else{
		if($width>$height){
			// calculate thumbnail size
			$newWidth = $minSize;
			$newHeight = floor( $height * ( $minSize / $width ) );
		}else{
			// calculate thumbnail size
			$newWidth = floor( $width * ( $minSize / $height ) );
			$newHeight =$minSize ;
		}
	}

	// create a new temporary image
	$tmp_img = imagecreatetruecolor( $newWidth, $newHeight );

	// copy and resize old image into new image
	imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

	$path = dirname(__FILE__) . "/$thumbId";
	// output the image 
	if(imagejpeg($tmp_img, $path)){
		$thumbLink = "";
		// upload thumbnail to s3
		if($s3->putObjectFile($path, $bucket, $thumbId, S3::ACL_PUBLIC_READ)){
			$thumbLink = $cloudFront.$thumbId;
			imagedestroy($tmp_img);
			unlink($path);
		}
		return $thumbLink;
		break;
	}
}

//include the S3 class
if (!class_exists('S3'))require_once('S3.php');

// include SimpleDB class only once
if (!class_exists('SimpleDB'))require_once('sdb.php');  

// Set awsAccessKey and awsSecretKey to your values
require_once('config.inc.php');  

//instantiate the S3 class
$s3 = new S3(awsAccessKey, awsSecretKey);

//instantiate the SimpleDB class
$sdb = new SimpleDB(awsAccessKey, awsSecretKey);

// Set temp directory where files will be written temporarily
$uploaddir = 'uploads/';

// Max file size 100 MB
$maxFileSize = 100 * 1024 * 1024;	

$thumb = '';
$status = '';
$imgWidth = '';
$imgHeight = '';

// Get file size from Apache headers
$fileSize = getSize();

// Get MIME type from Apache headers
$fileType = getFileType();

if ($fileSize == 0){
	return array(success=>false, error=>"File is empty.");
}				
if ($fileSize > $maxFileSize){
	return array(success=>false, error=>"File is too large.");
}

// Put data of pathinfo() array into $pathinfo	
$pathinfo = pathinfo(getName());

// Get file name - eg: myphoto
$filename = $pathinfo['filename'];

// Get extension - eg: .jpg
$ext = $pathinfo['extension'];
$originalName = $filename.'.'.$ext;

// Generate unique id for the current object
$randName = uniqid();		

// Unique file name with extension
$fileTempName =  $randName . '.' . $ext;	

// Complete temp file name and path
$fullTempName = $uploaddir . $fileTempName;

// Upload the file to temp directory on .net server
save($fullTempName);

// If images, call the function imgThumbs() to generate thumbnails
if(preg_match("/^image/", $fileType)){
	if(isset($_GET['profile_pic'])){
		$tbnail = $_GET['avatar_width'];
		$avatar_url = imgThumbs($tbnail, $fullTempName, $fileType, $bucket, $cloudFront);
	}
	$tbnail = $_GET['thumb_width'];
	$thumb = imgThumbs($tbnail, $fullTempName, $fileType, $bucket, $cloudFront);
	list($imgWidth, $imgHeight) = getimagesize($fullTempName);
}


// Metadata for SimpleDB
$contentObjectType = "upload";
$timeStamp = time();
$url = $cloudFront.$fileTempName;
$on_floor = "true";

/*
* An array of (name => (value [, replace])),
* where replace is a boolean of whether to replace the item.
* replace is optional, and defaults to false.
* If value is an array, multiple values are put.
*/
$putAttributesRequest = array(
	"contentid" 		=> array("value" => "$randName"),			// unique id for EVERY object and link
	"content_obj_type" 	=> array("value" => "$contentObjectType"),	// whether link or file upload
	"file_name" 		=> array("value" => "$fileTempName"),		// unique generated filename
	"url" 				=> array("value" => "$url"),				//file's CDN url
	"original_name"		=> array("value" => "$originalName"),		//original name of the file
	"file_size" 		=> array("value" => "$fileSize"),			//size of file uploaded
	"time_stamp" 		=> array("value" => "$timeStamp"),			//time
	"file_type" 		=> array("value" => "$fileType"),			//mime type of uploaded file
	"thumb" 			=> array("value" => "$thumb"),				//thumbnail link
	"avatar_url" 			=> array("value" => "$avatar_url"),				//thumbnail link
	"width" 			=> array("value" => "$imgWidth"),			//width of uploaded image
	"height" 			=> array("value" => "$imgHeight"),			//height of uploaded image
	"on_floor"			=> array("value" => "$on_floor")			//by default all cObj on floor
);

// Get ALL the parameter hash passed
$contentObjHash = getParam();										
foreach($contentObjHash as $key => $value){
	$putAttributesRequest["$key"] = array("value" => "$value");
}	

//check whether a form was submitted
if(isset($fileTempName)){

	// Begin object hash here
	$objHash = '{';

	/* Move the file to S3
	 * 
	 * @param mixed $fileTempName Location of temp file
	 * @param string $bucket Bucket
	 * @param string $newFileName Unique generated file name
	 * @param constant ACL
	 * @param array() Dont worry about this
	 * @param string $fileType MIME type of file
	 * @return boolean
	 */ 
	if(!$s3->putObjectFile($fullTempName, $bucket, $fileTempName, S3::ACL_PUBLIC_READ, array(), $fileType)) {
		$status = 'false';
		$objHash .= "success : ".json_encode($status)."}";			// End object hash here id S3 error
		echo $objHash;				
		return;
	}

	/**
	* Create or update attributes
	*
	* @param string $domain Domain
	* @param string $randName Unique generated file name
	* @param array $putAttributesRequest See up for more info
	* @return boolean
	*/										
	if($sdb->putAttributes($domain, $randName, $putAttributesRequest)){
		$status = 'true';
		unlink($fullTempName);
	}else{
		$status = 'false';
		$objHash .= "'SimpleDB_error' : ".json_encode($sdb->ErrorCode).",";
	}

	foreach($putAttributesRequest as $key => $value){
		$objHash .= json_encode($key). " : " . json_encode($value["value"]) .", ";
	}	

	$objHash .= "'success' : ".$status."}";				// End object hash after SimpleDB transaction

	echo $objHash;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.