Jump to content

Upload image + resize + thumbnail


austinnnnn

Recommended Posts

So i have a website, here i have a backoffice. In the backoffice i can add new "places" and when i add a new place i want to be able to upload 1 picture of it. All good till now.

 

So i have a from, and i want the image i upload do be resized to 800x600, and to create a thumbnail of 200px x 200px. So can someone help me? What is the best way to do this? Just store the path of the 800x600 image on the db and save the thumbnail with the same name of the 800x600 image but with thum in the front? Or save both paths in the db?

 

Because the main goal is to show the place in the frontpage, like this:

 

The thumb with the name of the place, and when i click on the thumb, i will get the 800x600 image with a lightbox.

 

Can someone help me please?

Link to comment
Share on other sites

This is what i got:

<?php 
include('dbconnect.php');
//ira verificar se clicou no botao enviar
if(isset($_POST['enviar'])){

		$nomesitio = $_POST['nome'];
		$horario = $_POST['horario'];
		$contato = $_POST['contato'];
		$morada = $_POST['morada'];
		$tipo = $_POST['tipo'];
		$freguesias = $_POST['freguesias']; 
                //o nome original do arquivo no computador do utilizador
         $arqName = $_FILES['arquivo']['name'];
        //o tipo mime do arquivo 
        $arqType = $_FILES['arquivo']['type'];
        //o tamanho do arquivo
        $arqSize = $_FILES['arquivo']['size'];
        //o nome temporario do arquivo com que foi guardado no servidor 
        $arqTemp = $_FILES['arquivo']['tmp_name'];
        //o codigo de erro associado a este upload de arquivo
        $arqError = $_FILES['arquivo']['error'];
        //lista de tipos de arquivos permidos
        $tiposPermitidos = array('image/gif', 'image/jpeg', 'image/jpg', 'image/png');
        //tamanho maximo
        $tamanhoPermitido = 6024 * 3000;
                if ($arqError == 0){
                                //verifica o tipo de arquivo enviado
                                if(array_search($arqType, $tiposPermitidos) === false){
                                        $retorno = '<span class="no">O tipo de arquivo enviado é invalido!</span>' ;
                                        //verifica o tamanho do arquivo enviado
                                }elseif ($arqSize > $tamanhoPermitido){
                                                $retorno = '<span class="no">O tamanho do arquivo enviado e maior que o limite!</span>';
                                }else{
                                        $pasta = 'images_sitios/';
                                        //pega a extensao do arquivo enviado
                                        $extensaoTemp = explode('.',$arqName);
                                        $extensao = strtolower(end($extensaoTemp));
                                        //define o nome do arquivo usando um Unix TimesTamp
                                        $nome = time(). '.' . $extensao;
                                        $upload = move_uploaded_file($arqTemp, $pasta . $nome);
                                                if ($upload === true){
                                                        $ficheiro = $pasta.$nome;                                                     
                                                }else{
                                                        $retorno = '<span class="no">Nao foi possivel fazer o upload da imagem!</span>';
                                                }
                                }               
                        }
        if(empty($ficheiro)){
                $retorno = '<span class="quase">Insira a imagem!</span>';
                }       
        //verifica se a variavel retorno estiver vazia (nao conter erro)        
                if(empty($retorno)){
        //cria uma query Mysql
        $query = mysql_query("INSERT INTO sitio (nome_sitio, horario, contato, morada, imagem, id_tipo, id_freguesia) VALUES ('$nomesitio','$horario','$contato','$morada','$ficheiro','$tipo','$freguesias')") or die (mysql_error());
        //se a query existe entao a pagina é redireccionada
                if($query === true){
                $sucesso = '<span  class="yes">Sitio inserido com sucesso!</span>';
                echo $sucesso;
            }
        }else{
                echo $retorno;
        }
        }
        ?>

The image is placed in the folder, and the path on db. I just want help on resizing the image to 800x600 or something, and to creathe the thumb for it. And also where to save the thumb path 

Link to comment
Share on other sites

this post might help: http://forums.phpfreaks.com/topic/268852-image-upload-validation-not-working/page-2?do=findComment&comment=1381699

 

just resize the image twice (process.php): (this script below also creates a randon name, its just a copy and past from one of my projects)

	// *** Create 'random number' + 'random_name' for image name
	$imagename = time() . '_' . mt_rand(1000,9999) . '_' . 'randon_name' . '.jpg';
	// What Directories to put the images
	$largelocation = '/home/public_html/images/';
	$thumblocation = '/home/public_html/images/thumb/';
	//thumbnail location
	$large = $largelocation . $imagename;
	$thumb = $thumblocation . $imagename;
	
	// *** 1) Initialise / load image
	$resizeObj = new resize($newPath);
	// *** 2) Resize LARGE image (options: exact, portrait, landscape, auto, crop)
	$resizeObj -> resizeImage(800, 600, 'auto'); //was 650, 487 wass 667, 500
	// *** 3) Save image + define quality
	$resizeObj -> saveImage($large, 85);
	
	// *** 4) Initialise / load image for second resize
	$resizeObj = new resize($newPath);
	// *** 5) Resize THUMB (options: exact, portrait, landscape, auto, crop)
	$resizeObj -> resizeImage(150, 100, 'crop'); //was 220, 165
	// *** 6) Save image + define quality
	$resizeObj -> saveImage($thumb, 85);
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.