使用 imagecopyresampled 在 Drupal 中调整上传图像的大小
我正在尝试调整通过表单上传到 Drupal 的图像的大小。我的代码是:
//Image resizing
//Get file and if it's not the default one - resize it.
$img = file_load($form_state['values']['event_image']);
if($img->fid != 1) {
//Get the image size and calculate ratio
list($width, $height) = getimagesize($img->uri);
if($width/$height > 1) {
$new_width = 60;
$new_height = $height/($width/60);
} else if($width/$height < 1) {
$new_height = 60;
$new_width = $width/($height/60);
} else {
$new_width = 60;
$new_height = 60;
}
//Create image
$image_p = imagecreatetruecolor($new_width, $new_height);
$ext = strtolower(pathinfo($img->uri, PATHINFO_EXTENSION));
if($ext == 'jpeg' || $ext == 'jpg') {
$image = imagecreatefromjpeg($img->uri);
} else {
$image = imagecreatefrompng($img->uri);
}
//Resize image
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save image as jpeg
imagejpeg($image_p, file_create_url($img->uri), 80);
//Clean up
imagedestroy($image_p);
//Store the image permanently.
$img->status = FILE_STATUS_PERMANENT;
}
file_save($img);
所以我想要实现的目标是将新文件(较小的大小)保存到上传的旧文件上。
我遇到的问题是 PHP 在 imagejpeg($image_p, file_create_url($img->uri), 80);
上抛出警告:
Warning: imagejpeg() [function. imagejpeg]:无法打开“http://localhost:8888/drupal/sites/default/files/pictures/myimage.png”进行写入:没有这样的文件或目录在 event_creation_form_submit()
因此,图像不会调整大小。有谁知道我做错了什么?
谢谢,
I'm trying to resize an image that is uploaded to Drupal via a form. The code I have for it is:
//Image resizing
//Get file and if it's not the default one - resize it.
$img = file_load($form_state['values']['event_image']);
if($img->fid != 1) {
//Get the image size and calculate ratio
list($width, $height) = getimagesize($img->uri);
if($width/$height > 1) {
$new_width = 60;
$new_height = $height/($width/60);
} else if($width/$height < 1) {
$new_height = 60;
$new_width = $width/($height/60);
} else {
$new_width = 60;
$new_height = 60;
}
//Create image
$image_p = imagecreatetruecolor($new_width, $new_height);
$ext = strtolower(pathinfo($img->uri, PATHINFO_EXTENSION));
if($ext == 'jpeg' || $ext == 'jpg') {
$image = imagecreatefromjpeg($img->uri);
} else {
$image = imagecreatefrompng($img->uri);
}
//Resize image
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save image as jpeg
imagejpeg($image_p, file_create_url($img->uri), 80);
//Clean up
imagedestroy($image_p);
//Store the image permanently.
$img->status = FILE_STATUS_PERMANENT;
}
file_save($img);
So what I'm trying to achieve is to save the new file (with the smaller size) over the old one that got uploaded.
The problem I'm getting is PHP throws a warning on imagejpeg($image_p, file_create_url($img->uri), 80);
saying:
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'http://localhost:8888/drupal/sites/default/files/pictures/myimage.png' for writing: No such file or directory in event_creation_form_submit()
Because of this, the image isn't resizing. Does anyone know what I'm doing wrong?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如克莱夫指出的那样 - 修复方法是使用 image_scale。这是工作代码的摘录:
As Clive pointed out - the fix was to use image_scale. Here is an extract of working code: