Brain dead... write image to file
| From: | Ryan S | Date: | Wed, 15 Oct 2008 08:51:16 +0000 |
| Subject: | Brain dead... write image to file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-281940@lists.php.net to get a copy of this message | ||
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
}
?>