Jump to content

Image Upload/Resize Problems


dokueki@gmail.com

Recommended Posts

Hey there. Sorry to be asking this as my first post, but here goes, hope you manage to figure out what went on here.

Basically I have a localhost website -- before I get a server I want a system properly running.

Anyways, I have an image upload form which should resize an image and copy it to a further resized thumbnail.

 

newphoto.template.php

<?php
print_r($_FILES);
if (isset($_POST['photo']))
{
	$photo = image_resize($_FILES['photo']['tmp_name'], 800, 600);
	$thumb = image_resize($_FILES['photo']['tmp_name'], 160, 120);

	mysql_query("INSERT INTO `photos` (`poster_id`) VALUES ('".$_COOKIE['id']."')");

	$result = mysql_query("SELECT `photo_id` FROM `photos` ORDER BY `photo_id` DESC LIMIT 1");
	$photo_id = mysql_fetch_row($result);

	$size = getimagesize($_FILES['photo']['tmp_name']);
	switch ($size['mime'])
	{
		case 'image/jpeg':
			imagejpeg($photo, 'uploads/photos/photo_'.$photo_id[0].'.jpg');
			imagejpeg($thumb, 'uploads/photos/thumbs/photo_'.$photo_id[0].'.jpg');
			break;
		case 'image/png':
			imagepng($photo, 'uploads/photos/photo_'.$photo_id[0].'.png');
			imagepng($thumb, 'uploads/photos/thumbs/photo_'.$photo_id[0].'.png');
			break;
	}


	if (!$_POST['more'])
	{
		redirect('./?p=img&id='.$photo_id[0]);
	}
}
?>
<form action="?p=newphoto" method="post" enctype="multipart/form-data">
<input type="file" name="photo" /><br />
<input type="checkbox" checked="checked" name="more" />Upload another photo afterwards<br />
<input type="submit" value="Upload" />
</form>

 

images.php

<?php
function image_resize($img_src, $new_w, $new_h)
{
$size = getimagesize($img_src);
print_r($size);
switch ($size['mime'])
{
	case 'image/jpeg':
		$old = imgcreatefromjpeg($img_src);
	case 'image/png':
		$old = imgcreatefrompng($img_src);
}

$ratio = ($size[0] > $size[1]) ? $w / $new_w : $h / $new_h; echo $ratio;
$w = $w / $ratio;
$h = $h / $ratio;
$img_dest = imagecreatetruecolor($w, $h);
imagecopyresampled($img_dest, $old, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

return $img_dest;
}
?>

 

Basically what happens, is, nothing. No file is uploaded, no SQL insertion is done (which leads me to believe the problem is in the image_resize function?). I'm rather new to image manipulating, this is the first method I tried creating using one. It's supposed to return a resized (keeping ratio), copied image of the source image provided. Maybe I didn't get the hang of GD functions right?

 

A few notes...

[*]Both files are included in other template files, which have DB access, and other functions.

[*]Ignore the fact I didn't validate correct file type, sizes, and if $_COOKIE['id'] actually exists, I'll do that once I'm sure everything works.

If you need any further information just say so, thanks in advance!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.