Jump to content

Recommended Posts

Hi guys, back yet again lol...

 

Just wondering how i would alter my code to make it resize images to 100 x 100 my code is below any help is appretiated.

 

$field = "avatar"; //Use field name for avatar
//Check that we have a file
if((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {
//Check if the file is JPEG/GIF and it's size is less than 15Kb
$filename = basename($_FILES['avatar']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "gif") && ($_FILES["avatar"]["type"] == "image/jpeg") || ($_FILES["avatar"]["type"] == "image/pjpeg") || ($_FILES["avatar"]["type"] == "image/gif") && 
//the size below this line states it must be smaller than 15Kb
($_FILES["avatar"]["size"] < 15000)) {
//the line below this gives the uploaded file a random number name
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$newplus=$random_digit.".";
$subavatar=$newplus.$ext;
$subavatar = stripslashes($subavatar);

//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/userimg/'.$subavatar;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['avatar']['tmp_name'],$newname))) {
$database->updateUserField($this->username,"avatar",$subavatar);
} else {
$form->setError($field, "* Error: A problem occurred during file upload!");
}
} else {
$form->setError($field, "* Error: File ".$_FILES["avatar"]["name"]." already exists");
}
} else {
$form->setError($field, "* Error: Only .jpg or .gif images please.");
}
} else {
$form->setError($field, "* Error: Your file was too big. It needs to be less than 15kb.");
}

 

Thanks Rick

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

<?php
function imageResizeWidth($iw, $sourcefile, $destfile){
$is = getimagesize($sourcefile);
$ih = $is[1] / ($is[0] / $iw);
$img_src = imagecreatefromjpeg($sourcefile);
$img_dst = imagecreatetruecolor($iw, $ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1]);
if(!imagejpeg($img_dst, $destfile, 90)) return FALSE;
else return TRUE;
}
function imageResizeHeight($ih, $sourcefile, $destfile){
$is = getimagesize($sourcefile);
$iw = $is[0] / ($is[1] / $ih);
$img_src = imagecreatefromjpeg($sourcefile);
$img_dst = imagecreatetruecolor($iw, $ih);
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $iw, $ih, $is[0], $is[1]);
if(!imagejpeg($img_dst, $destfile, 90)) return FALSE;
else return TRUE;
}
?>

 

Those two functions let you re size by either height or width.

 

So you can set as 100px wide and use the second function, and it will retain the aspect ratio!

<?php
/**
* Make thumbs from JPEG, PNG, GIF source file
*
* $tmpname = $_FILES['source']['tmp_name'];   
* $size - max width size
* $save_dir - destination folder
* $save_name - tnumb new name
*
* Author:  LEDok - http://www.citadelavto.ru/
*/

function img_resize( $tmpname, $size, $save_dir, $save_name )
    {
    $save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
        $gis       = GetImageSize($tmpname);
    $type       = $gis[2];
    switch($type)
        {
        case "1": $imorig = imagecreatefromgif($tmpname); break;
        case "2": $imorig = imagecreatefromjpeg($tmpname);break;
        case "3": $imorig = imagecreatefrompng($tmpname); break;
        default:  $imorig = imagecreatefromjpeg($tmpname);
        }

        $x = imageSX($imorig);
        $y = imageSY($imorig);
        if($gis[0] <= $size)
        {
        $av = $x;
        $ah = $y;
        }
            else
        {
            $yc = $y*1.3333333;
            $d = $x>$yc?$x:$yc;
            $c = $d>$size ? $size/$d : $size;
              $av = $x*$c;        //высота исходной картинки
              $ah = $y*$c;        //длина исходной картинки
        }   
        $im = imagecreate($av, $ah);
        $im = imagecreatetruecolor($av,$ah);
    if (imagecopyresampled($im,$imorig , 0,0,0,0,$av,$ah,$x,$y))
        if (imagejpeg($im, $save_dir.$save_name))
            return true;
            else
            return false;
    }

?>

 

Usage:

<?php
$field = "avatar"; //Use field name for avatar
//Check that we have a file
if((!empty($_FILES["avatar"])) && ($_FILES['avatar']['error'] == 0)) {
//Check if the file is JPEG/GIF and it's size is less than 15Kb
$filename = basename($_FILES['avatar']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "gif") && ($_FILES["avatar"]["type"] == "image/jpeg") || ($_FILES["avatar"]["type"] == "image/pjpeg") || ($_FILES["avatar"]["type"] == "image/gif") &&
//the size below this line states it must be smaller than 15Kb
($_FILES["avatar"]["size"] < 15000)) {
//the line below this gives the uploaded file a random number name
$random_digit=rand(0000,9999);

//combine random digit to you file name to create new file name
//use dot (.) to combile these two variables
$newplus=$random_digit.".";
$subavatar=$newplus.$ext;
$subavatar = stripslashes($subavatar);

//Determine the path to which we want to save this file
$dir = dirname(__FILE__).'/userimg/';
$newname = $dir . $subavatar;

//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if (img_resize($filename, 100 ,$dir , $subavatar)) {
$database->updateUserField($this->username,"avatar",$subavatar);
} else {
$form->setError($field, "* Error: A problem occurred during file upload!");
}
} else {
$form->setError($field, "* Error: File ".$_FILES["avatar"]["name"]." already exists");
}
} else {
$form->setError($field, "* Error: Only .jpg or .gif images please.");
}
} else {
$form->setError($field, "* Error: Your file was too big. It needs to be less than 15kb.");
}
?>

 

Un sure if that will work but that is the basic gist.

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.