Re: Brain dead... write image to file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ryan S wrote:
Hey all,

am feeling a bit brain dead, pulled an all nighter and would appreciate some help as have already wasted over 2hrs on this :(
just not thinking straight.
I got this script off the net, cant even remember where :( its basically to resize an uploaded image (i have a script that does exactly this that i created ages ago... but cant find it and if i remember corrrectly that only resizes jpgs, this one does gifs and pngs as well)

the way the original guy wrote it is that it writes the uploaded image to the DB, i'm just trying for it to create a resized image *instead of writing to the db*.

The code is below, needless to say, my little block of code to write an the image to file is not working... would appreciate some help.

<?php //$conn = mysql_connect("localhost", "username", "password") or die(mysql_error()); //mysql_select_db('test', $conn) or die(mysql_error());

if($_FILES['userfile']['tmp_name']){ resize(); //$q = "INSERT INTO test VALUES ('', '".$blob_arr['image']."', '".$blob_arr['thumb']."', '".$blob_arr['type']."')"; // $aa = mysql_query($q,$conn) or die(mysql_error());
/////////########## Start my code try
$uploaddir = 'C:\\wamp\\www\\ezee\\funny\\';
$filename = $uploaddir.'test.jpg';
$somecontent = $blob_arr['thumb'];

if (is_writable($filename)) {
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }
   if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    fclose($handle);

} else {    echo "The file $filename is not writable";}



header( "Content-type: ".$blob_arr['type'].""); }
/////////########## End my code try



function resize(){ global $blob_arr; $temp_name =$_FILES['userfile']['tmp_name']; $userfile_name =$_FILES['userfile']['name']; $userfile_size =$_FILES['userfile']['size']; $userfile_type =$_FILES['userfile']['type']; $thumb_width=90; $thumb_height=90; $image_width=200; $image_height=200; if (!($userfile_type =="image/pjpeg" OR $userfile_type =="image/jpeg" OR $userfile_type=="image/gif" OR $userfile_type=="image/png" OR $userfile_type=="image/x-png")){ die ("You can upload just images in .jpg .jpeg .gif and .png format!<br / >"); } $data = fread(fopen($temp_name, "rb"), filesize($temp_name)); $src_image = imagecreatefromstring($data); $width = imagesx($src_image); $height = imagesy($src_image); if ($thumb_width && ($width < $height)) { $thumb_width = ($thumb_height / $height) * $width; } else { $thumb_height = ($thumb_width / $width) * $height; } if ($image_width && ($width < $height)) { $image_width = ($image_height / $height) * $width; } else { $image_height = ($image_width / $width) * $height; } $dest_img = imagecreatetruecolor($thumb_width, $thumb_height); $i_dest_img = imagecreatetruecolor($image_width, $image_height); imagecopyresized($dest_img, $src_image,0, 0, 0, 0,$thumb_width, $thumb_height,$width, $height); imagecopyresized($i_dest_img, $src_image,0, 0, 0, 0,$image_width, $image_height,$width, $height); ob_start(); if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){ imagejpeg($dest_img); } if($userfile_type == "image/gif"){ imagegif($dest_img); } if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){ imagepng($dest_img); } $binaryThumbnail = ob_get_contents(); ob_end_clean(); ob_start(); if($userfile_type == "image/jpeg" OR $userfile_type == "image/pjpeg"){ imagejpeg($i_dest_img); } if($userfile_type == "image/gif"){ imagegif($i_dest_img); } if($userfile_type == "image/png" OR $userfile_type == "image/x-png"){ imagepng($i_dest_img); } $binaryImage = ob_get_contents(); ob_end_clean(); if(!get_magic_quotes_gpc()){ $binaryThumbnail=addslashes($binaryThumbnail); $binaryImage=addslashes($binaryImage); } $blob_arr['image']=$binaryImage; $blob_arr['thumb']=$binaryThumbnail; $blob_arr['type']=$userfile_type; } if($_POST['submit']){ // $Rs = mysql_query("SELECT * FROM funny_test WHERE id = '".mysql_insert_id()."'") or die(mysql_error()); // $row_Rs = mysql_fetch_assoc($Rs); // header( "Content-type: ".$row_Rs['typ'].""); // echo $row_Rs['thb'];









}else{ ?> <form name="form1" method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>"> <input id="userfile" name="userfile" type="file" /> <input type="submit" name="submit" value="Submit" /> </form> <?php } ?>

Wow, that's one ugly ass script!
Basically, to answer your question: use the SECOND parameter to imagejpeg/imagepng/imagewhatever. The 2nd parameter takes a string which denotes the path to the file it should store it in.
This should work a bit better for you:
<?php
function resizeImage($origPath, $destPath=null, $outputType='png') {
	// dimensions of the thumbnail to be created
	// we will retain aspect-ratio later on
	$destHeight = 90;	// pixels
	$destWidth  = 90;	// pixels
	
	$origImage  = file_get_contents($origPath);
	
	// reading original failed
	if(false === $origImage) {
		return false;
	}
	
	$origStream = imagecreatefromstring($origImage);
	
	// unsupported image type provided
	if(false === $origStream) {
		return false;
	}
	
	// calculate thumbnail size, retaining aspect ratio
	$origHeight = imagesy($origStream);
	$origWidth  = imagesx($origStream);
	
	if( ($origHeight/$origWidth) > ($destHeight/$destWidth) ) {
		$destWidth  = ($destHeight/$origHeight)*$destWidth;
	} else {
		$destHeight  = ($destWidth/$origWidth)*$destHeight;
	}
	
	$destStream = imagecreatetruecolor($destWidth, $destHeight);
	
imagecopyresampled($destStream, $origStream, 0, 0, 0, 0, $destWidth, $destHeight, $origWidth, $origHeight);
	
	// write the image
	switch($outputType) {
		case 'png':
			imagepng($destStream, $destPath);
			break;
		case 'jpg':
		case 'jpeg':
			imagejpeg($destStream, $destPath);
			break;
		case 'gif':
			imagegif($destStream, $destPath);
			break;
	}
	
	// free memory
	imagedestroy($destStream);
	imagedestroy($origStream);
	
	return true;
}

This function is slightly easier for you to use. pass in the original path to the image, 2nd param determines if it should be saved anywhere or directly sent to output (by passing null. Be sure to set the correct header first though)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux