How i can resize Images with imagescale in PHP
i tried to resize an image with following method:
code
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
Actual its saves my images in the original width and height. But why? Whats wrong?
php image
|
show 1 more comment
i tried to resize an image with following method:
code
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
Actual its saves my images in the original width and height. But why? Whats wrong?
php image
1
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
We don't even know$this->openImage()
. And it's off-topic (why isn't code working).
– digijay
Dec 31 '18 at 11:40
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
1
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by@
, remove them.
– digijay
Dec 31 '18 at 11:46
1
Please note that file systems can be case sensitive, so usingstrtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.
– Arjan
Dec 31 '18 at 11:47
|
show 1 more comment
i tried to resize an image with following method:
code
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
Actual its saves my images in the original width and height. But why? Whats wrong?
php image
i tried to resize an image with following method:
code
$thumbimg = $img->resizeImage($thumb, 200, 200);
public function resizeImage($imagePath,$new_width,$new_height) {
$fileName = pathinfo($imagePath,PATHINFO_FILENAME);
$ext = pathinfo($imagePath,PATHINFO_EXTENSION);
$fullPath = pathinfo($imagePath,PATHINFO_DIRNAME)."/".$fileName.'.'.$ext;
if (file_exists($fullPath)) {
return $fullPath;
}
$image = $this->openImage($imagePath);
if ($image == false) {
return null;
}
$width = imagesx($image);
$height = imagesy($image);
$imageResized = imagecreatetruecolor($width, $height);
if ($imageResized == false) {
return null;
}
$image = imagecreatetruecolor($width , $height);
$imageResized = imagescale($image,$new_width,$new_height);
touch($fullPath);
$write = imagepng($imageResized,$fullPath);
if (!$write) {
imagedestroy($imageResized);
return null;
}
imagedestroy($imageResized);
return $fullPath;
}
Actual its saves my images in the original width and height. But why? Whats wrong?
php image
php image
edited Dec 31 '18 at 11:28
Sanu0786
544715
544715
asked Dec 31 '18 at 11:24
Daniel RichterDaniel Richter
117
117
1
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
We don't even know$this->openImage()
. And it's off-topic (why isn't code working).
– digijay
Dec 31 '18 at 11:40
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
1
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by@
, remove them.
– digijay
Dec 31 '18 at 11:46
1
Please note that file systems can be case sensitive, so usingstrtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.
– Arjan
Dec 31 '18 at 11:47
|
show 1 more comment
1
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
We don't even know$this->openImage()
. And it's off-topic (why isn't code working).
– digijay
Dec 31 '18 at 11:40
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
1
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by@
, remove them.
– digijay
Dec 31 '18 at 11:46
1
Please note that file systems can be case sensitive, so usingstrtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.
– Arjan
Dec 31 '18 at 11:47
1
1
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
We don't even know
$this->openImage()
. And it's off-topic (why isn't code working).– digijay
Dec 31 '18 at 11:40
We don't even know
$this->openImage()
. And it's off-topic (why isn't code working).– digijay
Dec 31 '18 at 11:40
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
1
1
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by
@
, remove them.– digijay
Dec 31 '18 at 11:46
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by
@
, remove them.– digijay
Dec 31 '18 at 11:46
1
1
Please note that file systems can be case sensitive, so using
strtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.– Arjan
Dec 31 '18 at 11:47
Please note that file systems can be case sensitive, so using
strtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.– Arjan
Dec 31 '18 at 11:47
|
show 1 more comment
1 Answer
1
active
oldest
votes
If $imagePath
refers to an existing image, then $fullPath
will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null
is returned. Either way, the image resize code is not used.
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986879%2fhow-i-can-resize-images-with-imagescale-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If $imagePath
refers to an existing image, then $fullPath
will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null
is returned. Either way, the image resize code is not used.
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
add a comment |
If $imagePath
refers to an existing image, then $fullPath
will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null
is returned. Either way, the image resize code is not used.
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
add a comment |
If $imagePath
refers to an existing image, then $fullPath
will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null
is returned. Either way, the image resize code is not used.
If $imagePath
refers to an existing image, then $fullPath
will refer to that same file. So either the file exists, in which case it is returned, or the file does not exist, in which case you can't open the image and null
is returned. Either way, the image resize code is not used.
answered Dec 31 '18 at 11:44
ArjanArjan
8,37612132
8,37612132
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
add a comment |
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
aaaah thx. i'll try this.
– Daniel Richter
Dec 31 '18 at 11:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53986879%2fhow-i-can-resize-images-with-imagescale-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you checked your logs for error messages?
– Difster
Dec 31 '18 at 11:30
We don't even know
$this->openImage()
. And it's off-topic (why isn't code working).– digijay
Dec 31 '18 at 11:40
yes, no errors @Difster
– Daniel Richter
Dec 31 '18 at 11:42
1
Rather add code to your question by using the edit button. And most probably you don't get errors because they are suppressed by
@
, remove them.– digijay
Dec 31 '18 at 11:46
1
Please note that file systems can be case sensitive, so using
strtolower()
might not be the best idea. You could actually end up changing a correct path to a non-existing one.– Arjan
Dec 31 '18 at 11:47