调整图像大小后,如何使用 javascript 在单独的窗口中打开图像?

发布于 2024-10-07 04:25:51 字数 455 浏览 3 评论 0原文

这是我必须为我重新调整图像大小的代码。我不知道该怎么做是让代码在调整大小后打开图像。

所以我希望它是

  1. 调整图像大小(它已经在这样做)。
  2. 调整图像大小后,在单独的窗口中打开它

这是为我调整图像大小的代码。

function resize(which, max) {
    var elem = document.getElementById(which);
      if (elem == undefined || elem == null) return false;
     if (max == undefined) max = 1024;
    if (elem.width > elem.height) {
      elem.width = max;  
     elem.height = 768;
    }
   }

This is the code that I have to re-size the image for me. What I don't know how to do is have the code open the image after it has been re-sized.

So what I wish for it to is is

  1. Resize the image(It is already doing this).
  2. Once image is re-sized, open it in a separate window

This is the code that re-sizes the image for me.

function resize(which, max) {
    var elem = document.getElementById(which);
      if (elem == undefined || elem == null) return false;
     if (max == undefined) max = 1024;
    if (elem.width > elem.height) {
      elem.width = max;  
     elem.height = 768;
    }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2024-10-14 04:25:51

该代码实际上并没有调整图片的大小,它只是改变了它的显示方式。如果图像是下载的或在单独的窗口中打开的,则它将是原始图像的尺寸。我认为没有任何方法可以通过 JavaScript 修改图像。

不过,您可以做的是找到一个可以为您执行此操作的程序,并通过 Ajax 调用来调用它。例如,我认为 Gimp 可以处理这个问题,如果您可以从 PHP 脚本调用它,那么您可以通过 Ajax 调用调用该 PHP 脚本并显示它。这是一个复杂的解决方案,但这是我能想到的最好的解决方案。

That code does not actually resize the picture, it just changes how it is displayed. If the image is downloaded or opened in a separate window, it will be the dimensions of the original image. I don't think there is any way of modifying the image through JavaScript.

What you could do, though, is find a program that can do that for you and call that through an Ajax call. For example, I think the Gimp can handle that, and if you can call that from a PHP script, then you can call that PHP script through an Ajax call and display that. It's kind of a complicated solution but that's the best I can think of.

流云如水 2024-10-14 04:25:51

正如 tjameson 指出的那样,JavaScript 并不会真正调整图像的大小。

如果您想将此图像带到新窗口,可以采用以下方法:

var im = document.getElementsByTagName('img')[0];
var w = window.open("");
var b = w.document.body;
b.appendChild(im);

如果您希望调整其显示大小,可以在此图像上应用调整大小功能。

As tjameson pointed out, JavaScript doesn't really resize your image.

If you want to take this image to a new window, here is a way to do it:

var im = document.getElementsByTagName('img')[0];
var w = window.open("");
var b = w.document.body;
b.appendChild(im);

You can apply the resize function on this image if you want it displayed resized.

野鹿林 2024-10-14 04:25:51

我将所需的代码附加到初始调整大小函数中

    function resize(which, max) 
    {
        var elem = document.getElementById(which);
        if (elem == undefined || elem == null) return false;
        if (max == undefined) max = 1024;
        if (elem.width > elem.height) 
        {
            elem.width = max;  
            elem.height = 768;
        }
        var f = window.open("", "_blank", "height=500, width=600,toolbar=0, menubar=0, scrollbars=1, resizable=1,status=0, location=0, left=10, top=10");
        f.document.title = elem.alt;
        f.document.body.innerHTML += '<img src="'+elem.src+'" height="'+elem.height+'" width="'+elem.width+'" alt="'+elem.alt+'">';


}

I appended the needed code to the initial resize function

    function resize(which, max) 
    {
        var elem = document.getElementById(which);
        if (elem == undefined || elem == null) return false;
        if (max == undefined) max = 1024;
        if (elem.width > elem.height) 
        {
            elem.width = max;  
            elem.height = 768;
        }
        var f = window.open("", "_blank", "height=500, width=600,toolbar=0, menubar=0, scrollbars=1, resizable=1,status=0, location=0, left=10, top=10");
        f.document.title = elem.alt;
        f.document.body.innerHTML += '<img src="'+elem.src+'" height="'+elem.height+'" width="'+elem.width+'" alt="'+elem.alt+'">';


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文