Complez script for image handling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Complez script for image handling

Post by Perfidus »

I'm working in the following script, it should take a picture from hard disk (uploaded by user) resize the picture, write some text in it (Like URL of the site) and store it. Create a thumbnail and store it also and write some datas about the picture also reference, in the database.
I think it should work, but I get an Internal Server Error.
Here is the code:

Code: Select all

<?php
$msg = "";
switch(!strcasecmp($_SERVER['REQUEST_METHOD'], "POST")) {

   case true:

	if(!isset($_FILES['image']) || $_FILES['image'] == "none" || $_FILES['image'] == "") {
		$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Seleccione sus fotografías pulsando -Examinar-</font>";
		break;
	}

	$tmp = getcwd()."/".$_FILES['image']['name'];

	if(!@move_uploaded_file($_FILES['image']['tmp_name'], $tmp)) {

		$msg = "<font color='#0000FF' size='1' face='Arial, Helvetica, sans-serif'>Ha ocurrido un error al subir la imagen</font>";
		break;
	}

	$fp = fopen($tmp, "rb");
	$str = fread($fp, filesize($tmp));

	fclose($fp);
	unlink($tmp);

	$im1 = ImageCreateFromString($str);
	$base="mm";
	$conexion=mysql_connect("mm","mm","mm");
	mysql_select_db($base,$conexion);
	$result = mysql_query("SELECT * FROM fotos");
	$num_rows = mysql_num_rows($result);
	$numref = ("$num_rows"+5001);
	$iniciales = "rkf";
	$Ref =$iniciales.$numref;
	$sql = mysql_query("SELECT * FROM fotos WHERE ref2= '$Ref'");
	while(mysql_num_rows($sql) != 0){
	$Ref =$iniciales.($numref++);break;
	}

	$imgname = $Ref."thumb_1";		$maxwidth = 300;
	$maxheight = 150;

	$width1 = ImageSX($im1);
	$height1 = ImageSY($im1);
	$width2 = $maxwidth;
	$height2 = floor(($width2 * $height1) / $width1);

	if($maxheight > 0 && $height2 > $maxheight) {

		$height2 = $maxheight;
		$width2 = floor(($height2 * $width1) / $height1);
	}

	$im2 = ImageCreateTrueColor($width2, $height2);
	ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);

	ImageJpeg($im2, "thumb/".$imgname.".jpg");		// Se copia en temp/
	$msg = "Ok";
	$im3 = ImageCreateFromString($str);

	$imgname2 = $Ref;
	$maxwidth2 = 800;
	$maxheight2 = 250;

	$width12 = ImageSX($im3);
	$height12= ImageSY($im3);
	$width22 = $maxwidth2;
	$height22 = floor(($width22 * $height12) / $width12);

	if($maxheight2 > 0 && $height22 > $maxheight2) {

		$height22 = $maxheight2;
		$width22 = floor(($height22 * $width12) / $height12);
	}

	$im4 = ImageCreateTrueColor($width22, $height22);
	ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);

	ImageJpeg($im4, "full/".$imgname2.".jpg");		// Se copia en temp/
	$sql2 = "INSERT INTO fotos (artista,banda,evento,fecha,fotografo,ciudad,ref_autor,recinto,ref2)". 
	"VALUES ('$artista','$banda','$evento','$fecha','$fotografo','$ciudad','$ref_autor','$recinto','$ref2')"; 
	$result2 = mysql_query($sql2) or die(mysql_error().'<p>'.$sql2.'</p>');
	$image_url = "http://www.mysite.com/full".$ref".jpg";
	$size = getimagesize($image_url); 
	$imagewidth = $size[0]; // Index 0
	// Asignando variables fijas
	$font_file = $_SERVER[DOCUMENT_ROOT]."/images/fonts/verdana.TTF";
	$font_size = 8;
	$angle = 0;
	$max_width = 300;

	// Asignando texto
	$texto = "www.mysite.com";

	// Calculate horizontal starting point for the text (discussed in step 3)
	$line_width = imagettfbbox($font_size, 0, $font_file, $texto);
	if($pos==1){$x_start="10";$y_start="10";}else if
	($pos==2){$x_start=($imagewidth-($line_width[2] - $line_width[0]));$y_start="10";}else if
	($pos==3){$x_start="10";$y_start="230";}else if
	($pos==4){$x_start=($imagewidth-($line_width[2] - $line_width[0]));$y_start="230";}
	header("Content-type: image/jpeg");
	$im = imagecreatefromJpeg("images/".$ref.".jpg");
	$red = imagecolorallocate($color);

	
	imagettftext($im, $font_size, $angle, $x_start, $y_start, $red, $font_file, $texto);

	
	imagejpeg($im);
	header("Location:../solo.php?fotoinsert=si");
	break;
}
?>
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

you need to be more specific with your error - for starters, do othe php files work?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

No, the same document contains the form, so it operates everything by itself. Just tell me if you see something crazy, because by the moment, the only crazy here its me.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

well run a little hello world php file and see if it works
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

Of course, PHP is installed and working properly on the server.
All the parts that I have put together in this script have been working also.
I think the mistake came when I tried to make a single script.
Originally it was 1 script to resize and create thumbnail.
Another to write data in database and calculate reference number.
And another more for writing address inside the picture.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

all the error says is "internal server error" ?
Perfidus
Forum Contributor
Posts: 114
Joined: Sun Nov 02, 2003 9:54 pm

Post by Perfidus »

I have been checking the logs in the server, and this is the error:
malformed header from script. Bad header=<br />
Post Reply