Jump to content

Recommended Posts

I've been having non-stop problems with creating an Image uploading script that resizes to a certain size and name and then inputs it into a database along with a bunch of other information.

 

I have it running perfectly on my own computer, running the latest version of MAMP w/ PHP v5.2.0 installed.

 

When I try to upload it to my server (running PHP v5.2.3), the first error I got was it wouldn't allow me to use exif_imagetype() to look at the image type, so I just replaced it with a substr(). The next problem is that, even though it uploads the original image, it doesn't resize the image. I'm not sure whether it is getting stuck on the Content-Type, or on the imagecreatetruecolor() (both of which have popped up as errors in various tests.

 

At the moment when I submit the script, it goes to a page with a broken-image (Due to some problem with the Content-Type part) rather than resizing and continuing to the index page. 

 

 

Here is the code below, any help would be supremely appreciated. Sorry if it's hard to follow without comments.  :-X

 

 

// Function out of functions.php

 

 

function func_upload_image($name, $dir, $newname)

{

 

$original_filename = basename($_FILES[$name]["name"]);

if (substr($original_filename , -4) == ".gif" || substr($original_filename, -4) == ".GIF")

{

$filename = "../" . $dir . $newname . ".gif";

$dbfilename = $dir . $newname . ".gif";

}

 

if (substr($original_filename, -4) == ".jpg" || substr($original_filename, -4) == ".JPG")

{

$filename = "../" . $dir . $newname . ".jpg";

$dbfilename = $dir . $newname . ".jpg";

}

 

$_FILES[$name]["tmp_name"];

 

move_uploaded_file($_FILES[$name]["tmp_name"], $filename);

 

$area = getimagesize($filename);

 

 

$width = 300;

$height = 120;

 

 

 

 

 

if (substr($filename, -4) == ".gif" || substr($filename, -4) == ".GIF")  {

 

header("Content-Type: image/gif");

 

list($width_orig, $height_orig) = getimagesize($filename);

 

$ratio_orig = $width_orig/$height_orig;

 

if ($width/$height > $ratio_orig) {

  $width = $height*$ratio_orig;

} else {

  $height = $width/$ratio_orig;

}

 

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromgif($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

 

imagegif($image_p, $filename, 80);

 

 

}

 

 

if (substr($filename, -4) == ".jpg" || substr($filename, -4) == ".JPG")  {

 

 

header("Content-Type: image/jpeg");

 

list($width_orig, $height_orig) = getimagesize($filename);

 

$ratio_orig = $width_orig/$height_orig;

 

if ($width/$height > $ratio_orig) {

  $width = $height*$ratio_orig;

} else {

  $height = $width/$ratio_orig;

}

 

 

$image_p = imagecreatetruecolor($width, $height);

$image = imagecreatefromjpeg($filename);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

 

imagejpeg($image_p, $filename, 80);

 

}

return $dbfilename;

 

 

}

 

 

 

 

 

// In addnews.php

 

if ($_FILES["news_img"]["name"] != "") {

$result = mysql_query("SELECT * FROM news ORDER BY id DESC");

$row = mysql_fetch_array($result);

$newid = $row["id"];

$newid++;

$img = func_upload_image("news_img", "images/news/", $newid);

 

}

 

$currentdate = date('Y-m-d H:i:s');

mysql_query("INSERT INTO news (postdate, body, img, ip, userid, lastuserid, lastip, lastdate, display) VALUES ('$currentdate', '$_POST[news_body]', '$img', '$_SERVER[REMOTE_ADDR]', '$_SESSION[id]', '$_SESSION[id]', '$_SERVER[REMOTE_ADDR]', '$currentdate', '$_POST[news_display]')");

 

header("Location: index.php");

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/55675-php-523-imagecreatetruecolor-problem/
Share on other sites

alter as needed see first if it works ok.

 



<?php

if(isset($_POST['Submit']))

{
$size = 150; // the thumbnail height

$filedir = 'pics/'; // the directory for the original image
$thumbdir = 'pics/'; // the directory for the thumbnail image
$prefix = 'small_'; // the prefix to be added to the original name

$maxfile = '2000000';
$mode = '0666';

$userfile_name = $_FILES['image']['name'];
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];

if (isset($_FILES['image']['name'])) 
{
	$prod_img = $filedir.$userfile_name;

	$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
	move_uploaded_file($userfile_tmp, $prod_img);
	chmod ($prod_img, octdec($mode));

	$sizes = getimagesize($prod_img);

	$aspect_ratio = $sizes[1]/$sizes[0]; 

	if ($sizes[1] <= $size)
	{
		$new_width = $sizes[0];
		$new_height = $sizes[1];
	}else{
		$new_height = $size;
		$new_width = abs($new_height/$aspect_ratio);
	}

	$destimg=ImageCreateTrueColor($new_width,$new_height)
		or die('Problem In Creating image');
	$srcimg=ImageCreateFromJPEG($prod_img)
		or die('Problem In opening Source Image');
	if(function_exists('imagecopyresampled'))
	{
		imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}else{
		Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
		or die('Problem In resizing');
	}
	ImageJPEG($destimg,$prod_img_thumb,90)
		or die('Problem In saving');
	imagedestroy($destimg);
}

echo '
<a href="'.$prod_img.'">
	<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'">
</a>';

}else{

echo '
<form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
<input type="file" name="image"><p>
<input type="Submit" name="Submit" value="Submit">
</form>';
}

?>

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.