Parth Mangukiya
Parth Mangukiya

Reputation: 454

How to save same image with different names in PHP?

I have written a function for creating a thumbnail from .jpg image. But what I want to do is whenever the function gets called, same image should be saved as 1.jpg,2.jpg,3.jpg at the same destination.

I have used Sessions and static variable concept but to no success.

Here is my code. this is thumbsave.php file

<?php
session_start();
$_SESSION['sid']=$k=1;
function createThumb($fpath)//fpath will be passed here as parameter.
{
$ims = imagecreatefromjpeg($fpath);
$imd = imagecreatetruecolor(100, 100);

imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), 
imagesy($ims));
imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");
$_SESSION['sid'] = $_SESSION['sid'] + 1;
imagedestroy($ims);
imagedestroy($imd);

echo "Thumbnail Created and Saved at the Destination";

}

?>

Here is my code for dynamicthumb.php

<?php
include("include/thumbsave.php");
createThumb("imgs/m1.jpeg");
 ?>

So,when I run the dynamicthumb.php file the image stored at the imgs folder must be stored inside saveimages folder. But here only 1 image is saved, not multiple copies are generated like 2.jpg,3.jpg.

Upvotes: 1

Views: 122

Answers (2)

IncredibleHat
IncredibleHat

Reputation: 4104

You can use a file_exists loop if you are positive the name of the thumbnail will always be numerical:

function createThumb($fpath)
{
    $ims = imagecreatefromjpeg($fpath);
    $imd = imagecreatetruecolor(100, 100);

    imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));

    $thumb = 1;
    while ( file_exists('saveimages/'. $thumb .'.jpg') ) { $thumb++; }

    imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
    imagedestroy($ims);
    imagedestroy($imd);

    echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}

However I question the logic behind what you are asking for... as this suggests that every thumbnail you create will just be a sequential number, all stored in the same directory?

You may have better luck using a counter db field or file, as doing file_exists on a folder with thousands and growing, can hurt performance.

So a file based counter solution could be thus:

function createThumb($fpath)
{
    $ims = imagecreatefromjpeg($fpath);
    $imd = imagecreatetruecolor(100, 100);

    imagecopyresized($imd, $ims, 0, 0, 0, 0, 100, 100, imagesx($ims), imagesy($ims));

    $thumb  = file_get_contents('saveimages/thumbcount.txt');
    $thumb++; file_put_contents('saveimages/thumbcount.txt',$thumb);

    imagejpeg($imd,'saveimages/'. $thumb .'.jpg');
    imagedestroy($ims);
    imagedestroy($imd);

    echo 'Thumbnail Created and Saved at the Destination as '. $thumb .'.jpg';
}

Just be sure to prime the thumbcount.txt with a 1 so it has something to begin with.

Upvotes: 1

Taha Paksu
Taha Paksu

Reputation: 15616

This is the line responsible for saving image to file:

imagejpeg($imd,"saveimages/" . $_SESSION['sid'] . ".jpg");

You can update it to use $fpath instead of ,$_SESSION['sid']:

imagejpeg($imd, $fpath);

But beware, your path should end with .jpg.

Upvotes: 0

Related Questions