Jump to content

Image Resize and upload issue (thumbs)


princeofpersia

Recommended Posts

Hi guys,

 

Im trying to resize and create thiumbnails while uploading an image but it doesnt work, as i am new to php and i have followed other tutorials, i might have made a mistake which i dont understand. i have a table called test (find attached image) and what i need is when i upload an image, it copies the location of both image and on fly created thumb into my database

 

i appreciate your help & please excuse me for messed up code

 

 

 

<?php
session_start();
include ("../global.php");
//welcome messaage
echo "Welcome, " .$_SESSION['username']."!<p>";
$username=$_SESSION['username'];
//get user id & credit limit
$query=mysql_query("SELECT id FROM users");
while($row = mysql_fetch_array($query))
{
$id = $row['id'];
echo $id;
}
$credit=mysql_query("SELECT credit FROM users");
while($row = mysql_fetch_array($credit))
{
$creditcheck = $row['credit'];
echo "your credit is: $creditcheck";
}
    $reference = rand(11111111,99999999);










     //image1
  define ("MAX_SIZE","100");
   define ("WIDTH","150");
define ("HEIGHT","100");
function make_thumb($img_nameone,$filenameone,$new_w,$new_h)
{
$ext=getExtension($img_nameone);
if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
$src_imgone=imagecreatefromjpeg($img_nameone);
if(!strcmp("png",$ext))
$src_imgone=imagecreatefrompng($img_nameone);
$old_x=imageSX($src_imgone);
$old_y=imageSY($src_imgone);
$ratio1=$old_x/$new_w;
$ratio2=$old_y/$new_h;
if($ratio1>$ratio2) {
$thumb_w=$new_w;
$thumb_h=$old_y/$ratio1;
}
else {
$thumb_h=$new_h;
$thumb_w=$old_x/$ratio2;
}
$dst_imgone=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_imgone,$src_imgone,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if(!strcmp("png",$ext))
imagepng($dst_imgone,$filenameone);
else
imagejpeg($dst_imgone,$filenameone);
imagedestroy($dst_imgone);
imagedestroy($src_imgone);
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }
$errors=0;
if ($creditcheck<=0)           
Header("Location: buy-credit.php");             
          else         
if ($creditcheck>=0){

if(isset($_POST['register']))
$submit = mysql_query("INSERT INTO test (reference) 
     VALUES 
     ('$reference')");
     





    {
$image=$_FILES['myfileone']['nameone'];
if ($image)
{
$filename = stripslashes($_FILES['myfileone']['nameone']);
$extension = getExtension($filenameone);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else {
$size=getimagesize($_FILES['myfileone']['tmp_name']);
$sizekb=filesize($_FILES['myfileone']['tmp_name']);
if ($sizekb > MAX_SIZE*1024)
{ echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; }
$image_nameone=time().'.'.$extension;
$newname="rentimages/".$image_nameone;
$copied = copy($_FILES['myfileone']['tmp_name'], $newname);

if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else {
$thumb_name='rentimages/thumbs/thumb_'.$image_name;
$thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT);
}} }}
if(isset($_POST['register']) && !$errors)
{


$locationone="rentimages/$nameone";

$image1 = mysql_query ("UPDATE test SET image1='$newname', thumb='$thumb_name' WHERE reference='$referenceran'");
}
}
}
?>



<html>
<body>




  <form action='imagetests.php' method='POST' enctype='multipart/form-data'>

     
File: <input type='file' name='myfileone'><p />

    <input type='submit' name='register' value='Update'> <p />


</body>
</html>

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/221521-image-resize-and-upload-issue-thumbs/
Share on other sites

I just include a pretty generic image resize script which I then call. If you would like this script, then let me know (on this board) and I'll email you it. Or if you follow me on Twitter (@manteya) then I will message you my email address.

Archived

This topic is now archived and is closed to further replies.

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