Craig Hopson Posted June 11, 2012 Share Posted June 11, 2012 hi guys have this code which works file but i need to rename the file after its converted (xxxx.png to xxxx.jpg) $originalimg = test.png$srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); Link to comment Share on other sites More sharing options...
ckrudelux Posted June 11, 2012 Share Posted June 11, 2012 hi guys have this code which works file but i need to rename the file after its converted (xxxx.png to xxxx.jpg)$originalimg = test.png $srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); http://se2.php.net/rename Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 11, 2012 Author Share Posted June 11, 2012 (edited) yes i looked at that but if the file name changes like $originalimg = $_GET['img']; $srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); how would i just change the extension from .png to .jpg but keep the file name the same Edited June 11, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
ckrudelux Posted June 11, 2012 Share Posted June 11, 2012 yes i looked at that but if the file name changes like$originalimg = $_GET['img']; $srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); how would i just change the extension from .png to .jpg but keep the file name the same I see, just rename it to jpg or jpeg instead png or just change the name then saving the file and remove the old one. Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 11, 2012 Author Share Posted June 11, 2012 ok i done this is it correct or is there a better way?? $srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); $NewName = str_replace(".png",".jpg",$originalimg); rename($originalimg,$NewName); Link to comment Share on other sites More sharing options...
ckrudelux Posted June 11, 2012 Share Posted June 11, 2012 ok i done this is it correct or is there a better way??$srcImg = imagecreatefrompng($originalimg); imagejpeg($srcImg,$originalimg,100); imagedestroy($srcImg); $NewName = str_replace(".png",".jpg",$originalimg); rename($originalimg,$NewName); What works until someone has .png in the filename and not as an extension. Try with explode and take away everything after the last dot. Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks do you have example Link to comment Share on other sites More sharing options...
ckrudelux Posted June 11, 2012 Share Posted June 11, 2012 Thanks do you have example $newfile = explode(".",$originalimg);array_pop($newfile);$newfile = implode(".",$newfile) . ".png";rename($originalimg, $newfile); 1 Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 11, 2012 Author Share Posted June 11, 2012 Thanks Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 12, 2012 Author Share Posted June 12, 2012 hi bit different problem but same topic so here goes, this code will change Avatar.png to Avatar1339516953.png $filename .= time(); how would i get 1339516953_Avatar.png Link to comment Share on other sites More sharing options...
birbal Posted June 12, 2012 Share Posted June 12, 2012 $filename=time() . '_avatar.png'; 1 Link to comment Share on other sites More sharing options...
thescientist Posted June 12, 2012 Share Posted June 12, 2012 (edited) hi bit different problem but same topic so here goes, this code will change Avatar.png to Avatar1339516953.png$filename .= time(); how would i get 1339516953_Avatar.png are you sure it wouldn't actually change it to Avatar.png1339516953? Unless there's something you're leaving out of the picture... to answer your question though,$filename = time() . '_' . $filename; Edited June 12, 2012 by thescientist Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 12, 2012 Author Share Posted June 12, 2012 (edited) yes there is another variable $ext holding the extension thanks i have sorted it like this $filenameA = time() . '_'.$filename;$filename = $filenameA; Edited June 12, 2012 by Craig Hopson Link to comment Share on other sites More sharing options...
Guest So Called Posted June 12, 2012 Share Posted June 12, 2012 Why the intermediate $filenameA ??? Link to comment Share on other sites More sharing options...
Craig Hopson Posted June 12, 2012 Author Share Posted June 12, 2012 cos that was the only way i could get it to do what i wanted Link to comment Share on other sites More sharing options...
boen_robot Posted June 12, 2012 Share Posted June 12, 2012 You can get the extension, as well as the base name and directory name of a file with the pathinfo() function, which should significantly ease what you're trying to do.You could just do: $filename_parts = pathinfo($filename, PATHINFO_BASENAME | PATHINFO_EXTENSION);$filename_parts['basename'] = time() . '_' . $filename_parts['basename'];$filename = implode('.', $filename_parts); Link to comment Share on other sites More sharing options...
Guest So Called Posted June 12, 2012 Share Posted June 12, 2012 cos that was the only way i could get it to do what i wanted Why not this?$filename = time() . '_'.$filename; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now