Jump to content

adding a resize to my image upload


jasonhardwick

Recommended Posts

ok i have two bits of code and need them to work together to resize then rename then upload the new file to a folder then update my database.

I've tried to put them together but when I do I loose my file ext

Resize code:

<?php

$user_image = $_FILES['user_image']['tmp_name'];

$src = imagecreatefromjpeg($user_image);

list($width,$height)=getimagesize($user_image);

$newwidth=250;
$newheight=($height/$width)*250;
$tmp=imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

$filename = "user_images/". $_FILES['user_image']['name'];
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); 
?>

 

here is my rename, upload and update code:

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$user_image = $rows['user_image'];

$new_name = $username.".";

$target = "user_images/";

$target = $target . $new_name.$ext;
if(move_uploaded_file($_FILES['user_image']['tmp_name'], $target)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

?>

 

Code I tried to put together:

<?php

$user_image = $_FILES['user_image']['tmp_name'];

$src1 = imagecreatefromjpeg($user_image);

list($width1,$height1)=getimagesize($user_image);

$newwidth1=250;
$newheight1=($height1/$width1)*250;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);

$filename1 =  $_FILES['user_image']['name'];


?>

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$ext = findexts ($_FILES[$filename1]['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;

if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 
imagedestroy($src1);
imagedestroy($tmp1); 
?>

 

Link to comment
https://forums.phpfreaks.com/topic/106571-adding-a-resize-to-my-image-upload/
Share on other sites

Here is a function to resize your images: http://phpsnips.com/snippet.php?id=5

 

I don't know how much it will help, so here is some code to implement it:

function getext($file){
    $pos = strrpos($file,'.');
    $str = substr($file,$pos);
    return strtolower($str);
} 


foreach ($_FILES["pictures"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
       $name = $_FILES["pictures"]["name"][$key];
       $ext = getext($name);
       $target = "user_images/";
       $target = $target . $_SESSION['username'].$ext;
       move_uploaded_file($tmp_name, "data/$name");
       createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);
       //120 = thumb width  ::  80 = thumb quality (1-100)
   }
} 

ok i'll go another route...

 

can anyone see why i'm loosing my $ext?

 

<?php

$user_image = $_FILES['user_image']['tmp_name'];

$src1 = imagecreatefromjpeg($user_image);

list($width1,$height1)=getimagesize($user_image);

$newwidth1=250;
$newheight1=($height1/$width1)*250;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);

$filename1 =  $_FILES['user_image']['name'];


?>

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$ext = findexts ($_FILES[$filename1]['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;

if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 
imagedestroy($src1);
imagedestroy($tmp1); 
?>

ok so i tried that move and now it dosent look like my php is being processed... all it does is go to my processing page but nothing is happening?

<?php

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$user_image = $_FILES['user_image']['tmp_name'];

$src1 = imagecreatefromjpeg($user_image);

list($width1,$height1)=getimagesize($user_image);

$newwidth1=250;
$newheight1=($height1/$width1)*250;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);

$filename1 =  $_FILES['user_image']['name'];

function findexts ($filename) 
{ 

$ext = findexts ($_FILES[$filename1]['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;

if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}


$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 
imagedestroy($src1);
imagedestroy($tmp1); 
?>

 

 

I think you did not understand what I meant (or I just said it wrong).  You need to move the entire function to the top.  I only stated part of the function, but I meant the entire thing.

<?php
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$ext = findexts ($_FILES[$filename1]['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;

if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

imagedestroy($src1);
imagedestroy($tmp1); 
?>

ok so i've put the new code up and run the edit image page try to upload and it give me this as an output:

+Y�X�������m#��d�hˣ�ڛ�.�`t�l�Аi�ڕ��W1]��w ��k��<��*����|�.$vVҒKX!���kT��-����4��WU�LJ� �����ګ�Rq�Z*��JmFJv�%e��WZ�{�;7%/�qYO	E��Td����Q�RW��5k�~h�� �Z���E��kg���bg��u%��2H���_���!�u����1?��L����ܒ,�᣷��DB#�8�R[yl�Ͳ��{�����J�U�-m�>�2[͵L�6�7K��Uh^��Ҳ��!%!B���WIG���D�Iko4n��kEu L��W�&X�G��*C ���z�9�Pu��F3�eV��O�)N<�q���rn�˕���a�TX|=59F�ᇩE>Y{H�w�5�e(�r�<֊��+ixO�z.�"�C�)�<��LY�2�],�c�2��E�W�խ����vёq���,��,g�eD-�+

 

here is the php:

<?php

// This is the temporary file created by PHP 
$user_image = $_FILES['user_image']['tmp_name'];

// Create an Image from it so we can do the resize
$src1 = imagecreatefromjpeg($user_image);

// Capture the original size of the uploaded image
list($width1,$height1)=getimagesize($user_image);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect 
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth1=250;
$newheight1=($height1/$width1)*250;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp1,$src1,0,0,0,0,$newwidth1,$newheight1,$width1,$height1);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename1 =  $_FILES['user_image']['name'];
//imagejpeg($tmp1,$filename1,100);


?>

<?php
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

include "config.php";
$tbl_name="user";
$username=$_SESSION['username'];

$ext = findexts ($_FILES[$filename1]['name']) ;

#$new_name = $rows['uname'].".";
$new_name = $username."."; //working replacement

imagejpeg($tmp1,$target,100);
$target = "user_images/";
$target1 = $target . $new_name.$ext;

if(move_uploaded_file($_FILES[$filename1]['tmp_name'], $target1)) 
{
$sql=( "UPDATE $tbl_name SET user_image = '".$new_name.$ext."' WHERE uname='$username'");
$rows = mysql_query($sql);
echo "Your profile image has been sucessfully updated";
$id = mysql_insert_id();
echo $id;

} 
else
{
echo "Sorry, there was a problem uploading your file.";
}

imagedestroy($src1);
imagedestroy($tmp1); 
?>

 

 

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.