473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how do I name an image when I create it?

So far I've got the code you see below. I've not yet figured out how
to change the name of the file. I'm creating several images of
different sizes. I need to give them all different names. How do I
control the name?

function makeImage($pathToImage=false, $desiredSize=false) {
if (file_exists($pathToImage)) {
if ($desiredSize) {
// 04-26-03 - This whole next section is off limits till I find out
how to test for image support in the
// PHP build in use - my own hosting company doesn't offer support
for functions like imagecreatefromjpeg.
//
// 05-25-03 - function_exists() is the correct way to test.
//
// 07-22-04 - when images are uploaded, a thumbnail should be
created.
//
// 11-16-04 - I'm going to try to revive this function today and
make it operational.
// It's been in the code but unused for a year and a half.
// Since most PHP installations don't have the GD library, I think
the way to go is
// to simply test for every function.

$controllerForAll = & getController();
$resultsObject = & $controllerForAll->getObject("McResults",
"makeImage");

if (function_exists("getimagesize")) {
if (function_exists("imagecreate")) {
if (function_exists("imagecreatefromjpeg")) {
if (function_exists("imagecopyresized")) {
if (function_exists("imagesx") && function_exists("imagesy")) {
if (function_exists("imagejpeg")) {
if (function_exists("imagecreatefromgif")) {
if (function_exists("imagegif")) {

$size = getimagesize($pathToImage);
// 04-13-03 - Now we make sure we only output the jpgs and
gifs
// 04-13-03 - this next line is to find out if we are
outputting jpg or gif
// WE ONLY OUTPUT JPG AND GIF
if ($size[2] == 1 || $size[2] == 2) {
$oldWidth = $size[0];
$oldHeight = $size[1];

// 11-20-04 - we reset the width to whatever the desired
size is.
// standardImageInsert calls this function 4 times with
values of
// 50, 100, 250, and 400. We then compare the desired size
to the
// old width to get a ratioOfChange which we can then
apply to
// the height to get a propotional change. The division
will
// leave us with a float, but we need an integer for the
// height value, so we hit the result with round().
$newWidth = $desiredSize;
$ratioOfChange = $oldWidth / $desiredSize;
$newHeight = $oldHeight / $ratioOfChange;
$newHeight = round($newHeight);

$newImage = imagecreate($newWidth, $newHeight);

if ($size[2] == 2) {
$srcImage = imagecreatefromjpeg($image);
imagecopyresized($newImage, $srcImage, 0, 0, 0, 0,
$newWidth, $newHeight, imagesx($srcImage), imagesy($srcImage));
imagejpeg($newImage);
} elseif ($size[2] == 1) {
$srcImage = imagecreatefromgif($image);
imageCopyResized($newImage, $srcImage, 0, 0, 0, 0,
$newWidth, $newHeight, imagesx($srcImage), imagesy($srcImage));
imagegif($newImage);
}
}
}
}
}
}
}
}
}
}
} else {
$resultsObject->error("In makeImage, the second parameter should
have told us what width was desired for this image, but it did not.",
"makeImage");
}
} else {
if ($pathToImage == "") {
$resultsObject->error("In makeImage(), the first parameter should
have told us where to find this image (what path to it), but it did
not.", "makeImage");
} else {
$resultsObject->error("In makeImage(), we were told to resize an
image called '$pathToImage' but the image didn't seem to exist on the
server.", "makeImage");
}
}
}
Jul 17 '05 #1
4 1709
lawrence wrote:
So far I've got the code you see below. I've not yet figured out how
to change the name of the file. I'm creating several images of
different sizes. I need to give them all different names. How do I
control the name?


The imagejpeg/gif/png ake an optional filename argument and output the
jpeg image to that file if it is given.
bool imagejpeg ( resource image [, string filename [, int quality]])
Your function can take the filename as an argument and store the image
to the fiven filename.
For example imagejpeg($image,$fname."-fmt-jpg".'jpg')

Other than that, you have far too many nested ifs .. cannot help but
think "d00d, you are screwed"

Jul 17 '05 #2
"lunatech" <r.*******@gmail.com> wrote in message news:<11*********************@z14g2000cwz.googlegr oups.com>...
Other than that, you have far too many nested ifs .. cannot help but
think "d00d, you are screwed"


What's a better way to handle the situation? You and I both know that
90% of the people out there won't be able to use this code because the
PHP GD library won't be installed. What is your concern about this
code? That it will run slowly? I'm under the impression that
function_exists() is a fast function. It's not as if it needs to do
any complex calculations.

Still, if you know of a more concise way to guarantee that my fuction
will never call a PHP function that is not installed, I'd like to know
it.

One of the web design companies I work with has their servers with
Interland. The server setup is a nightmare. It's running Free BSD but
its a virtual environment such that its been hard for our local linux
gurus to upload a new RPM of PHP. For that reason they are still
running PHP 4.0.6 on their servers. For that reason, since they are
my biggest client, when I use functions in GD or PHP > 4.0.6 I tend to
test for the existence of the function.

It's a bad situation, but how else to deal with it? They are moving
all their clients to some new servers they've leased through
Rackspace, and Rackspace offers a much better setup. Till then, we
need to proceed carefully, writing PHP code that looks like Javascript
code, in the sense that we test excessively for the existence of
functions.
Jul 17 '05 #3
lawrence wrote:
"lunatech" <r.*******@gmail.com> wrote in message news:<11*********************@z14g2000cwz.googlegr oups.com>...
Other than that, you have far too many nested ifs .. cannot help but think "d00d, you are screwed"


What's a better way to handle the situation? You and I both know that
90% of the people out there won't be able to use this code because

the PHP GD library won't be installed. What is your concern about this
code? That it will run slowly? I'm under the impression that
function_exists() is a fast function. It's not as if it needs to do
any complex calculations.
My concern is that the code is too complex. But that maybe just
because I have a small brain :-)

Still, if you know of a more concise way to guarantee that my fuction
will never call a PHP function that is not installed, I'd like to know it.
Yes, I think you can handle the above code more elegantly. From a
cursory glance it seems that

- All the functions (imagesx/y, imagecreatefromjpeg, imagejpeg etc.)
*must* exist to make your function work correctly.

- you call a function *only* when it is guranteed to exist

- You do not have an else part i.e. if some function is not found, you
bail out.

My solution is as follows

function do_required_image_functions_exist()
{
if (
function_exists(getimagesize) &&
function_exists(getimagecreate) &&
function_exists(magecreatefromgif) &&
function_exists(BLAH_BLAH)
)
return true;
else
return false;
}


function makeImage($pathToImage=false, $desiredSize=false)
{
if (do_required_image_functions_exist())
{
YOUR CODE
}

else
{
HANDLE ERROR CONDITION
}
}


One of the web design companies I work with has their servers with
Interland. The server setup is a nightmare. It's running Free BSD but
its a virtual environment such that its been hard for our local linux
gurus to upload a new RPM of PHP.


In a vhost, you can compile and install your own version of PHP. Why
do you need RPMs for FreeBSD. maybe you should contact your hosting
provider for further guidance. From what I know, it can be done but
YMMV

Jul 17 '05 #4
lk******@geocities.com (lawrence) wrote in message news:<da**************************@posting.google. com>...
So far I've got the code you see below. I've not yet figured out how
to change the name of the file. I'm creating several images of
different sizes. I need to give them all different names. How do I
control the name?


1. Using md5() on parameters. eg. $image_name =
'img'.md5($x.$y.$size).'.jpg';
//imgacbd18db4cc2f85cedef654fccc4a4d8.jpg
2. http://in.php.net/uniqid

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg | last post by:
looking for an easy way for users to browse for a file on their local drive, and then have access automatically make a copy of that file (files will most likely be images) and save it under a new...
15
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and...
12
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses...
2
by: Mango | last post by:
Hi there, I will be very happy if someone helps me. I need to create dynamic image, and save the state between the postback. I mean I've got 5 buttons and when I click on one of them, the image is...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
1
by: Karl | last post by:
Can you create name tags with pictures using A2000? I have a table with an Employee name field and a field with a link to their gif picture. I was able to get a form to work using the Image...
2
by: Kevin Walzer | last post by:
I'm trying to avoid a *lot* of typing in my Tkinter application by associating image names with items in a list. Here is my sample list: self.catlist = I've also already created a bunch of...
0
by: u01jmg3 | last post by:
Hi, basically I create a png image on the fly and that all works fine but when I go to save the image it's taking the name of the .php script used to create it as its default name. e.g. I visit...
10
by: Keith G Hicks | last post by:
I'm hoping there's a simple way to do this. I need to show a dummy image in an asp image object if the file is missing. Here's my asp.net 2.0 markup: <asp:Image ID="imgGrad" runat="server"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.